winform处理耗时操作

gudicao gudicao 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
程序是扫描局域网里的主机:

public delegate void OnUiControl();
private void button1_Click(object sender, EventArgs e)
{
Thread thScan = new Thread(new ThreadStart(DoControl));
thScan.IsBackground = true;
thScan.Start();
}
private void DoControl()
{
OnUiControl uicontrol = this.ScanTarget;
if (this.InvokeRequired)
{
this.Invoke(uicontrol);
}
}

private void ScanTarget()
{
string strIPAddress = "172.30.81."; //ip段
int nStrat = Int32.Parse("2"); //开始扫描地址
int nEnd = Int32.Parse("50"); //终止扫描地址
//开始扫描(耗时操作)
for (int i = nStrat; i <= nEnd; i++)
{
string strScanIPAdd = strIPAddress + i.ToString();
IPAddress myScanIP = IPAddress.Parse(strScanIPAdd); //转换成IP地址
try
{
//当执行下面这个操作时候比较耗时
IPHostEntry myScanHost = Dns.Resolve(strScanIPAdd);
string strHostName = myScanHost.HostName;
richTextBox1.AppendText(strHostName + "\r");
Application.DoEvents();

}
catch (Exception error)
{
MessageBox.Show(error.Message);
}
}
}


上面是原程序,为什么使用了多线程后界面还会卡死呢?求解
...全文
评论 16
收藏 已收藏
分享
返回顶部
gudicao
C# 111,130 成员 642,541 内容