c#如何使用线程而不阻塞主界面

来源:互联网 发布:淘宝买东西怎么付款 编辑:程序博客网 时间:2024/04/30 19:35

主线程中点击某button(name为simpleButtonCopy)


 public delegate void NoObjectDel();

 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;

}