. NET parallel computing and concurrent 3.2-multithreading invoke Invoke

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.