C # main thread auxiliary thread

Source: Internet
Author: User

Main thread:
Is the UI thread;
From the main thread through thread. start or asyncdelegate. beginenvoke (which can contain parameters, define callback methods, and avoid polling) will enter the auxiliary thread. Calling the delegate in asynchronous mode can prevent the worker thread from being blocked during interface re-painting;
Calls the delegated asyncdelegate from the main thread. the endenvoke method will enter the auxiliary thread (endinvoke () is the blocking method, and calling endinvoke In the callback method can obtain the return value of the asynchronous call method), and wait until the end.

Auxiliary thread:
Is the worker thread;
The slave thread calls the controls of any control. invoke method or controls. begininvoke can be switched back to the main thread, and the object array parameters in invoke/begininvoke () are passed to the delegate parameters for the delegate to be executed as parameters.
Remember: The callback method in the asyncdelegate. beginenvoke parameter is executed in the auxiliary thread rather than in the main thread. This means that you should never try to update the UI in a similar callback method! (This means that the synchronous call method of the callback method is also executed in the auxiliary thread)

The delegate called by the invoke and begininvoke methods of the control is executed in the UI thread in any way.
In the delegate callback, endinvoke is called to prevent the main thread from running. That is to say, the main thread will not be executed until the begininvoke thread is executed completely.
The thread of the callback method and the thread of the delegate method are on the same thread.

Run in new threadProgram(In the following example, compute is a class that encapsulates time-consuming operations, usually compute. genxlrz is a long task that needs to be executed in the auxiliary thread. In genxlrz, the event update is triggered by calling onupdate ;):
Compute c = new compute ();
C. Update + = new compute. updateeventhandler (c_update); // C. Update sends event notifications to the main thread, which is usually used to update the interface.

// Method 1 directly use threadstart
// Thread t = new thread (New threadstart (C. genxlrz ));
// T. Start ();

// Method 2 is flexible, and a callback method can be provided.
// Genxlrzdelegate genxlrz = new genxlrzdelegate (C. genxlrz );
// Genxlrz. begininvoke (new system. asynccallback (this. enablecontrol), null); // The callback enablecontrol is also run in the new thread through the delegate asynchronous call.

// Method 3 Use threadpool, which is relatively simple
Waitcallback async = new waitcallback (C. genxlrz );
Threadpool. queueuserworkitem (async, null );
Emphasize again: begininvoke's callback method this. enablecontrol: C. in the genxlrz method, the response method c_update that triggers the event runs on the auxiliary thread, that is, the UI control cannot be directly accessed (through control. invoke access-this method is always switched to the main thread to execute the delegate method)

 

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.