The following example uses a background thread to perform the calculation logic, which does not affect the foreground interface operation, which means
You can perform other actions in the foreground UI interface.
The focus is on the new thread, which calls a delegate method, which is required to populate the data to the foreground control because
The foreground control is created in the original thread, so the Invoke method needs to be called in the new thread, and the real-time display
The calculation progress of the background logic.
The code is as follows:
1 PublicPartialclassForm1:form2{3 Private Delegate voidMyDelegate (Longj);4 PrivateMyDelegate Dele;5 PublicForm1 ()6{7InitializeComponent ();8Dele + =NewMyDelegate (Delegatemethod);9}Ten One Private voidDelegatemethod (LongJ A{ - This. TextBox1.Text = J.tostring (); -} the - Private voidButton1_Click (Objectsender, EventArgs e) -{ -Thread THD =NewThread (Threadmethod); +Thd. Start (); -} + A Private voidThreadmethod () at{ - Longj = 0; - for(inti = 0; i < 1e10; i++) -{ -j + +; - if(j%1000 = = 0) in{ - //After invoking the Invoke method, the Delegatemethod method is executed in the original thread that created the textbox1. to This. Invoke (DELE,J); +} -} the} *} $
View Code
. NET parallel computing and concurrent 3.2-multithreading invoke Invoke