C # summary of how the sub-thread updates the UI control,

Source: Internet
Author: User

C # summary of how the sub-thread updates the UI control,

Controls are often updated in child threads in winform C/S programs. The UI thread of the desktop program is the main thread, when you try to directly modify the control attributes from a subthread, an error message "access the control from a thread that is not creating the control" appears.

There are two common methods to update the UI control across threads:

1. Use the control's own invoke/BeginInvoke Method

2. Update using the Post/Send method of SynchronizationContext

 

1. Use the control's own invoke/BeginInvoke Method

The Control class implements the ISynchronizeInvoke interface. We can see the definition of this interface:

The invoke method of the Control class has two implementations.

Object Invoke (Delegate); // execute the specified Delegate on the thread that owns the basic window handle of this control

Object Invoke (Delegate, Object []);

We can see that all the Control-inherited UI controls can be updated asynchronously using the Invoke method. The following code snippet updates the Text attribute of the Label control in the Child thread.

 

[Csharp]View plaincopy
  1. Private void button6_Click (object sender, EventArgs e)
  2. {
  3. Thread demoThread = new Thread (new ThreadStart (threadMethod ));
  4. DemoThread. IsBackground = true;
  5. DemoThread. Start (); // Start the thread
  6. }
  7. Void threadMethod ()
  8. {
  9. Action <String> AsyncUIDelegate = delegate (string n) {label1.Text = n ;};/<span style = "font-family: Arial, Helvetica, sans-serif; ">/define a DeleGate </span>
  10. Label1.Invoke (AsyncUIDelegate, new object [] {"modified label1 text "});
  11. }


2. Update using the Post/Send method of SynchronizationContext

The SynchronizationContext class provides a free thread context without synchronization in the System. Threading command space. The Post method signature is as follows:

Public virtual void Post (SendOrPostCallback d, Object state) // schedule asynchronous messages to a synchronization Context

We can see that we want to asynchronously update the UI control. The first is to obtain the context of the UI thread, and the second is to call the post method to implement the Code:

 

[Csharp]View plaincopy
    1. SynchronizationContext _ syncContext = null;
    2. Private void button6_Click (object sender, EventArgs e)
    3. {
    4. Thread demoThread = new Thread (new ThreadStart (threadMethod ));
    5. DemoThread. IsBackground = true;
    6. DemoThread. Start (); // Start the thread
    7. }
    8. // Form Constructor
    9. Public Form1 ()
    10. {
    11. InitializeComponent ();
    12. // Obtain the UI thread synchronization Context
    13. _ SyncContext = SynchronizationContext. Current;
    14. }
    15. Private void threadMethod ()
    16. {
    17. _ SyncContext. Post (SetLabelText, "modified text"); // update the UI through the UI thread context in the Child thread
    18. }
    19. Private void SetLabelText (object text)
    20. {
    21. This. lable1.Text = text. ToString ();
    22. }

C Language & |! What are

& Is the address fetch operator used to extract the address of a variable.
For example, if you define a variable, the system will allocate a space in the memory during compilation.
The location of the space in the memory is its address. & Extract its address.
E. g int a; assign an address to it during compilation, for example, 2000; & a is 2000.
If an integer pointer Variable p, p = & a; is defined, the address 2000 of a is assigned to p. P = 2000 after running.
Another example is scanf ("% d", & a). When you enter 3, it first knows the address of a according to & a, and finds the space of a in the memory by the address, write 3 to this space.
* Is a pointer operator, which is opposite to &. It extracts the value of a Variable Based on the address of the variable.
For example, * the value of a is 3 of variable.
The following is a summary of the pointer used in the definition and description.
Int * p; defines a pointer to integer data.
Int * p [n]; defines the pointer array p, which consists of n pointer elements pointing to integer data.
Int (* p) [n]; p is the pointer variable pointing to a one-dimensional array containing n elements.
Int * p (); p is the function that returns a pointer pointing to integer data.
Int (* p) (); p is the pointer to the function. This function returns an integer value.
Int ** p; p is a pointer variable that points to an integer Data Pointer variable.
If you want to learn more about the system, you can refer to tan haoqiang's c Programming (the third edition), which is easy to understand. Is a good C language learning material.

C Language & |! What are

& Is the address fetch operator used to extract the address of a variable.
For example, if you define a variable, the system will allocate a space in the memory during compilation.
The location of the space in the memory is its address. & Extract its address.
E. g int a; assign an address to it during compilation, for example, 2000; & a is 2000.
If an integer pointer Variable p, p = & a; is defined, the address 2000 of a is assigned to p. P = 2000 after running.
Another example is scanf ("% d", & a). When you enter 3, it first knows the address of a according to & a, and finds the space of a in the memory by the address, write 3 to this space.
* Is a pointer operator, which is opposite to &. It extracts the value of a Variable Based on the address of the variable.
For example, * the value of a is 3 of variable.
The following is a summary of the pointer used in the definition and description.
Int * p; defines a pointer to integer data.
Int * p [n]; defines the pointer array p, which consists of n pointer elements pointing to integer data.
Int (* p) [n]; p is the pointer variable pointing to a one-dimensional array containing n elements.
Int * p (); p is the function that returns a pointer pointing to integer data.
Int (* p) (); p is the pointer to the function. This function returns an integer value.
Int ** p; p is a pointer variable that points to an integer Data Pointer variable.
If you want to learn more about the system, you can refer to tan haoqiang's c Programming (the third edition), which is easy to understand. Is a good C language learning material.

Related Article

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.