Summary of methods for updating UI controls by C # Child threads

Source: Internet
Author: User

http://blog.csdn.net/jqncc/article/details/16342121

In the WinForm C/S program, where the control is often updated in a child thread, the desktop program UI thread is the main thread, and an exception prompt "Access it from a thread that is not a control" appears when attempting to modify a control property directly from a child thread.

There are two common ways to update UI controls across threads:

1. Using the Invoke/begininvoke method of the control itself

2. Update using SynchronizationContext's Post/send method


1. Using the Invoke/begininvoke method of the control itself

The control class implements the ISynchronizeInvoke interface, and we look at the definition of that interface:

The Invoke method of the control class has two implementations

Object Invoke (Delegate); //Executes the specified delegate on the thread that owns the underlying window handle of this control

Object Invoke (delegate,object[]);

You can see that UI controls that inherit the control class can be updated asynchronously using the Invoke method. The following code snippet implements updating the Text property of a label control in a 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 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 SynchronizationContext's Post/send method

The SynchronizationContext class, under the System.Threading command space, provides a free-threaded context without synchronization, where the Post method is signed as follows:

public virtual void Post (SendOrPostCallback d,object State) //Dispatch asynchronous message to a synchronization context

We can see that we want to update the UI control asynchronously, the first is to get the context of the UI thread, and the second is to call the Post method, and the code implements:

[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 thread
    7. }
    8. Form Constructors
    9. Public Form1 ()
    10. {
    11. InitializeComponent ();
    12. //Get UI thread synchronization context
    13. _synccontext = synchronizationcontext.current;
    14. }
    15. Private void Threadmethod ()
    16. {
    17. _synccontext.post (Setlabeltext, "Revised text"); Updating UI through UI thread context in child threads
    18. }
    19. Private void Setlabeltext (object text)
    20. {
    21. This.lable1.Text = Text.  ToString ();
    22. }

Summary of methods for updating UI controls by C # Child threads

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.