happmaoo 发表于 2013-1-19 04:09:10

c#多线程处理界面

主线程中点击某button
private void simpleButtonCopy_Click(object sender, EventArgs e)
{
   this.simpleButtonCopy.Enabled = false;
   this.simpleButtonCopy.Text = "复制中...";
   Thread t1 = new Thread(new ThreadStart(CopyPics));
   t1.Start();
}
线程运行函数
private void CopyPics()
{
if (this.simpleButtonCopy.InvokeRequired)
   {
         BeginInvoke(new NoObjectDel(FinishCopyPics));
   }
   else
   {
         FinishCopyPics();
    }
}
private void FinishCopyPics()
   {
   this.simpleButtonCopy.Text = "复制";
   this.simpleButtonCopy.Enabled = true;
   }
调用函数可以简化下
private void CopyPics()
{
if (this.simpleButtonCopy.InvokeRequired)
{
this.Invoke(new NoObjectDel(CopyPics));
return;
}
this.simpleButtonCopy.Text = "复制";
this.simpleButtonCopy.Enabled = true;
}
页: [1]
查看完整版本: c#多线程处理界面