毕业论文开发语言企业开发JAVA技术.NET技术WEB开发Linux/Unix数据库技术Windows平台移动平台嵌入式论文范文英语论文
您现在的位置: 毕业论文 >> net技术 >> 正文

C#如何判断定线程执行完了

更新时间:2014-3-7:  来源:毕业论文

下面这段代码怎么才能保证线程执行完了,主线程再执行,请测试通过了再说。
因为这个比较特殊,线程中要访问控件,就使得次程要切换到主线访问控件。所以
使用 thread.Join(), ManualResetEvent 等方法 都会使程序死掉。


一个Form 窗体,btnTest 按钮 txtMsg文本框
 public partial class Form1 : Form     {           Thread thread;           public Form1()         {             InitializeComponent();         }                private void btnTest_Click(object sender, EventArgs e)         {                          thread = new Thread(new ThreadStart(DoWork));             thread.Start();                          //......               txtMsg.Text = "完成";           }           void DoWork()         {             SetText("线程运了。");            }           delegate void SetTextHandler(string msg);         private void SetText(string msg)         {             if (txtMsg.InvokeRequired)             {                 SetTextHandler d = new SetTextHandler(SetText);                 object val = (object)msg;                 this.Invoke(d, val);             }             else            {                 txtMsg.Text += "\r\n" + msg;             }         }     }
  public partial class Form1 : Form     {         public Form1()         {             InitializeComponent();         }           private void btnClick_Click(object sender, EventArgs e)         {             var callback = new Action(CallBack);               var thread = new Thread(DoSomething);             thread.Start(callback);         }           private void DoSomething(object obj)         {             var delegateMethod=new SetText(Set);             this.Invoke(delegateMethod, "楼主,干活好累!!!");                          Thread.Sleep(2000);//模拟延时。               (obj as Action)();         }           private void CallBack()         {             var delegateMethod = new SetText(Set);             this.Invoke(delegateMethod, "楼主,干完了!!!");              }           private delegate void SetText(string msg);         private void Set(string message)         {             this.lblMsg.Text = message;         }       }

设为首页 | 联系站长 | 友情链接 | 网站地图 |

copyright©youerw.com 优尔论文网 严禁转载
如果本毕业论文网损害了您的利益或者侵犯了您的权利,请及时联系,我们一定会及时改正。