C # multi-threaded control instance (invoke, control delegate method)

Source: Internet
Author: User
Using system; using system. collections. generic; using system. componentmodel; using system. data; using system. drawing; using system. text; using system. windows. forms; using system. threading; namespace treadtest {// defines the delegate Public Delegate void listboxdelegate (); Public partial class form1: FORM {public form1 () {initializecomponent ();} // delegate handling method (associated with listboxdelegate) Private void ListBox () {If (! Listbox1.invokerequired) // if you operate ListBox In the UI main thread, {listbox1.items. add (++ commondata. num); // controls are directly operated. "associated with the main UI thread" listbox1.selecteditem = listbox1.items [listbox1.items. count-1];} else // if you operate ListBox in another thread, enable delegate listbox1.invoke (New listboxdelegate (listshow ));} // define the operations on the main program-controlled parts of the UI, "associated with addauto ". Private void listshow () {listbox1.items. add (commondata. num); listbox1.selecteditem = listbox1.items [listbox1.items. count-1];} // defines the thread function private void addauto () {While (commondata. flag = 0) {commondata. num ++; thread. sleep (1000); ListBox (); // you cannot directly control controls on the UI, therefore, use this method to enable the multi-threaded private void btnstart_click (Object sender, eventargs E) in the click event. {// set the thread flag to 0, indicating that the thread commondata is enabled. flag = 0; // defines the threadstart delegate type parameter and directs the delegate to the thread function listboxdelegate mycn = new listboxdelegate (addauto ); // instantiate thread inserttxt = new thread (New threadstart (mycn); // start thread inserttxt. start ();} private void btnabort_click (Object sender, eventargs e) {commondata. flag = 1;} private void btnctrlmain_click (Object sender, eventargs e) {ListBox ();} private void btnreset_click (Object sender, eventargs e) {commondata. num = 0;} private void btnclear_click (Object sender, eventargs e) {listbox1.items. clear ();} private void btnquit_click (Object sender, eventargs e) {application. exit () ;}// global variable solution public class commondata {Private Static int _ flag = 0; Private Static int _ num = 0; public static int flag {get {return _ flag;} set {_ flag = value ;}} public static int num {get {return _ num ;} set {_ num = value ;}}}}

Summary:

To use a multi-threaded UI control, you must use a delegate. Call the control's invoke method (the invoke method parameter is a delegate type parameter ).

Steps:

1. Declare delegation.

2. Declare the delegate processing function (determine whether the main thread controls the UI control or the invoke (multi-thread) controls the UI control ).

3. Declare a thread instance and pass the thread function delegate to threadstart ().

4. Enable this thread.

5. Define the thread function. To control the UI control, call the delegate processing function in step 1 and use invoke for his own judgment.

6. Define the functions to be called by invoke (such as the listshow function in this example)

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.