Multi-threaded cross-domain access

 1  private void Button1_Click(object sender, EventArgs e)
 2         {
 3             Thread myThread = new Thread(Add);
 4             myThread.IsBackground = true;
 5             myThread.Start();
 6         }
 7 
 8         private void Add()
 9         {
10             for (int i = 0; i < 10000000; i++)
11             {
12                 if(label1.InvokeRequired) // determine whether or not to cross-thread access 
13                  {
 14                      // the Invoke method: find label1 create a thread, and then thread calls the method delegate pointed
 15                      // use the generic type parameter constraint 
16                      label1.Invoke ( new new the Action <the label, String > (Write), Label1, i.ToString ());
 . 17                  }
 18 is              }
 . 19          }
 20 is          Private  void Write (the label label, String value)
 21 is          {
 22 is              Label.text = value;
 23 is          }

 

Guess you like

Origin www.cnblogs.com/birdGe/p/11858639.html