About cross-thread access to controls in WinForm

Source: Internet
Author: User

Idle nothing to think up with WinForm to do a random number of procedures, I see this thing is not difficult, but for a rookie is not simple! Especially for the novice who is not particularly familiar with multithreading.

First, the interface is relatively simple WinForm, (picture upload good trouble ~~! )

Since it is a random number, you have to have a random number (in fact, the key is not here, the figure is random)

But when it comes to multithreading, the problem arises:

The label control cannot be accessed across threads, and the text of the label cannot be modified.

Find a lot of information on the Internet, feel the example made difficult to understand, so he lifted up ....

1. Someone wrote:

Control.checkforillegalcrossthreadcalls = false;
Label.checkforillegalcrossthreadcalls = false;

Personally feel that this is not appropriate to do so, so give up! (There are many reasons)

2. Reference material-commissioned by:

Try to use the way to solve the Commission, but I do not know why, the use of random numbers after the scroll, the last direct interface card dead, (seems to have no effect ╮ (╯-╰) ╭)

3. Use the Invok to transfer the event transmission method. As shown in the code

 One  A namespaceLottery Program - { -      Public Partial classForm1:form the     { -  -  -          PublicForm1 () +         { - InitializeComponent (); +         } A         BOOLStateflag =false;//Pause Identity atRandom r =NewRandom ();//Random Number Object -  -         Private voidStartnum_click (Objectsender, EventArgs e) -         { -             if(!Stateflag) -             { inStartnum.text ="Pause";//Change button text -Stateflag =true; toThread th =NewThread (Startrandom);//Open Thread +Th. IsBackground =true;//Identify Background Threads - th. Start (); the  *             } $             ElsePanax Notoginseng             { -Startnum.text ="Start"; theStateflag =false; +             } A  the  +         } -         /// <summary> $         ///generate a random number method, use the control's Invoke method to refresh the label, implement the digital scrolling $         /// </summary> -         Private voidstartrandom () -         { the             intR1; -             intR2;Wuyi             intR3; the              while(Stateflag) -             { WuR1 = R.next (0,Ten);//generate a random number of Label1 -r2 = R.next (0,Ten);//generate a random number of Label2 AboutR3 = R.next (0,Ten);//generate a random number of label3 $                 if(Label1. invokerequired) -                 { -                      This. Label1. Invoke (NewAction<label,string> (SetValue), This. Label1, R1. ToString ()); -                 } A                 if(Label2. invokerequired) +                 { the                      This. Label2. Invoke (NewAction<label,string> (SetValue), This. Label2, R2. ToString ()); -                 } $  the                 if(label3. invokerequired) the                 { the                      This. label3. Invoke (NewAction<label,string> (SetValue), This. LABEL3, R3. ToString ()); the                 } -             } in         } the  the         /// <summary> About         ///Change the label number the         /// </summary> the         /// <param name= "Lab" ></param> the         /// <param name= "Lbvalue" ></param> +         Private voidSetValue (Label Lab,stringlbvalue) -         { theLab. Text =Lbvalue;Bayi         } the  the  -         Private voidForm1_Load (Objectsender, EventArgs e) -         { the             //control.checkforillegalcrossthreadcalls = false; turn off cross-thread detection ...  the             //label.checkforillegalcrossthreadcalls = false; theStartnum.text ="Start"; the         } -     } the}

Summarize:

In fact, the principle of 2, 3 is basically the same, and then find the problem when found in fact is the use of the delegate when the Code misplaced position (roll to wall!). )

Control can only be accessed by the thread that created it. Other threads want to access the Invoke method that must call the control. Invoke has two parameters, one is the delegate method, and the other is the parameter value.

Whether it's an event or a delegate (casually).

Rookie first note, written relatively rotten, do not turn not to spray, this is dedicated to the novice

About cross-thread access to controls in WinForm

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.