当前位置:编程学习 > C#/ASP.NET >>

C#终止线程实例

子线程里有循环,如何立即终止线程
private void button1_Click(object sender, EventArgs e)
{
  label10.Text = "正在打开目标地址,请稍后..";
  label10.Refresh();
  button1.Enabled = false;
  isAbort = false;
  thread2 = new Thread(post);
  thread2.Start();
}
<span id="more-682"></span>
/// <summary>
/// 回帖操作
/// </summary>
private void post()
{
  ....
  //这里是循环回帖
  for(...)
  {
  if (isAbort)
return;
  ...
  }
}
 
//终止线程
private void button2_Click(object sender, EventArgs e)
{
  //停止工作
  try
  {
  isAbort = true;
  button1.Enabled = true;
  webBrowser.Visible = false;
  label10.Text = "停止运行";
  label10.Refresh();
  }
  catch
  {
  MessageBox.Show("已经是停止状态");
  }
}

摘自 http://www.xssxss.com/易做图/682.xss
补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,