General method for changing the attributes of a UI control in a non-UI thread

Source: Internet
Author: User

In. if you need to change the UI control attributes in a non-UI thread, the CLR will throw an exception and prompt that the control (cross-thread operation not valid) on the interface cannot be updated in a non-UI thread ). Generally, there are two solutions. The first is to set the static property checkforillegalcrossthreadcils of control to false, as follows:

View Source

Print?

01 Public Form1 ()
02 {
03 Initializecomponent ();
04 Control. checkforillegalcrossthreadcils =False;
05 }
06  
07 Private Void Button#click (Object Sender, eventargs E)
08 {
09 Thread thread =New Thread () =>
10 {
11 For (Int I = 0; I <100000; I ++)
12 {
13 Label1.text = I. tostring ();
14 Label1.refresh ();
15 }
16 });
17 Thread. Start ();
18 }

Another method is to use delegation to determine whether the update operation of the current control is in another thread Based on the invokerequired property of the control. If yes, use the delegate to call the method and update the control. However, this method has the disadvantage of creating separate delegates and methods for the attribute setting method of each control. These delegates and methods are only used for cross-thread operations. For example, when you need to modify the text of a label in another thread, you need to create a setlabeltext method. Suppose you still need to update the text of the textbox, you need to create another settextboxtext method.

Through the following delegation and method definition, we implement "one definition, multiple use ". See:

View Source

Print?

01 Private Delegate Void Parameterizedcontrolupdate (Params Object[] ARGs );
02  
03 Private Delegate Void Controlupdatedelegate (component C,
04 Parameterizedcontrolupdate callback,
05 Params Object[] ARGs );
06  
07 Private Void Delegatedcontrolupdate (component C,
08 Parameterizedcontrolupdate callback,
09 Params Object[] ARGs)
10 {
11 Control Target = (CIs Control )? (CAs Control ):This;
12 If (Target. invokerequired)
13 {
14 Controlupdatedelegate d = delegatedcontrolupdate;
15 Target. Invoke (D,New Object[] {C, callback, argS });
16 }
17 Else
18 {
19 Callback (ARGs );
20 }
21 }

The preceding example can be changed:

View Source

Print?

01 Private Void Button#click (Object Sender, eventargs E)
02 {
03 Thread thread =New Thread () =>
04 {
05 For (Int I = 0; I <100000; I ++)
06 {
07 Delegatedcontrolupdate (label1, argS =>
08 {
09 Label1.text = (String) ARGs [0];
10 Label1.refresh ();
11 }, I. tostring ());
12 }
13 });
14 Thread. Start ();
15 }

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.