线程内打开窗体

最近做了个线程,想在线程中打开窗体,结果总是一闪就木有了。在忘了看了下,原来是线程结束后会回收资源。把打开行窗体的资源回收了。

所以在要加点东东:

  1. private void Form1_Load(object sender, EventArgs e)  
  2.         {  
  3.             int threadId = Thread.CurrentThread.ManagedThreadId;  
  4.             textBox1.Text = threadId.ToString(); //将主线程ID显示在文本框中  
  5.         }  
  6.   
  7.         private void button1_Click(object sender, EventArgs e)  
  8.         {  
  9.             Thread thread2 = new Thread(threadPro);//创建新线程  
  10.             thread2.Start();  
  11.         }  
  12.         public void threadPro()  
  13.         {  
  14.             textBox2.Text = Thread.CurrentThread.ManagedThreadId.ToString();  
  15.              MethodInvoker MethInvo = new MethodInvoker(ShowForm2);  
  16.              BeginInvoke(MethInvo);  
  17.         }  
  18.         public void ShowForm2()  
  19.         {  
  20.             Form2 f2 = new Form2();  
  21.             f2.Show();  
  22.         } 
posted @ 2014-04-10 15:29  丰叔(◕ˇ◞◟ˇ◕)  阅读(191)  评论(0编辑  收藏  举报