异步委托实现多线程winform控件编程

   private   void  button1_Click( object  sender, EventArgs e)
        {
            ThreadStart ts 
=   new  ThreadStart(add);
            Thread th 
=   new  Thread(ts);
            th.Start();
        }

        
#region  异步委托实现多线程winform控件编程

        
delegate   void  changText( string  ss);

        
void  add()
        {
            
int  a  =   1 ;
            
int  b  =   2 ;
            
string  sum  =  Convert.ToString(a  +  b);
            
this .BeginInvoke( new  changText(intoText), sum);  // 计算完成需要在一个文本框里显示
        }

        
void  intoText( string  sum)
        {
            
if  ( this .InvokeRequired)
            {
                
this .BeginInvoke( new  changText(intoText), sum);
            }
            
else
            {
                textBox1.Text 
=  sum;
            }
        }

        
#endregion

你可能感兴趣的:(WinForm)