【发布时间】:2020-08-10 14:26:12
【问题描述】:
我有一个简单的工作要做,点击按钮它将从一个范围内获取所有网络 IP 地址,循环通过它们并将活动放在一个列表中。在执行该过程时,将显示一个面板,其中将显示正在检查的 IP 地址。代码运行良好,但表单挂起,应用程序没有响应,即使面板没有显示,IP 地址也没有显示。怎么做? 我的代码是:
private void btnAutoSearch_Click(object sender, EventArgs e)
{
panel_Search.Visible = true; // Not Working
Cursor.Current = Cursors.WaitCursor;
string ipBase = getIPAddress();
string[] ipParts = ipBase.Split('.');
ipBase = ipParts[0] + "." + ipParts[1] + "." + ipParts[2] + ".";
for (int i = 1; i < 255; i++)
{
string ip = ipBase + i.ToString();
Ping p = new Ping();
PingReply pingresult = p.Send(ip, 100);
if (pingresult.Status == IPStatus.Success)
{
lstIPs.Add(ip);
lblConnecting.Text = ip; // Not Working
}
}
GridConn.Rows.Clear();
foreach (string s in lstIPs)
{
Device obj = new Device();
obj.IPAddress = s;
lblConnecting.Text = s;
int vSMSLimit = 0;
int.TryParse(txtSMSLimit.Text, out vSMSLimit);
obj.SMSLimit = 0;
if (obj.ConnectToHostServer())
{
obj.SendConnectionMessage();
obj.ReceiveConnectionMessage();
MyDevices.lst.Add(obj);
GridConn.Rows.Add(true, obj.IPAddress, obj.PhoneModel, obj.PhoneStatus, obj.SoftwareVersion);
}
}
Cursor.Current = Cursors.Default;
panel_Search.Visible = false;
}
【问题讨论】:
-
使用
BackgroundWorker将网络通信移出 UI 线程 -
感谢您的帮助。
标签: c# winforms loops user-interface