C # simple implementation of multi-threaded interface controls

Source: Internet
Author: User
A small feature, which has already been implemented. I used it in a project. I think it is necessary to record it and write it down. Code

/* C #2005 and later do not support multi-thread direct access to interface controls (the interface creation thread is not the same as the access thread ),
* However, the following delegate-like solution can be used. */
Private   Delegate   Void Showmessage ( String Message );

Private   Void Showfeedback ( String Feedback)
{
If ( ! This . Rtbmsg. invokerequired) // Directly operate controls when they are not accessed in the thread
{
This . Rtbmsg. Text + = Feedback;
}
Else   // In-process access
{
Showmessage func =   New Showmessage (showfeedback );
This . Begininvoke (func, New   Object [] {FEEDBACK });
}
}

From the above, you may have seen how to operate on the same control through multiple threads, that is, through a delegate, and then define the delegate method to determine the control's invokerequired attribute (the metadata of this attribute is very well described, "because the call orientation is outside the thread where the control is created", it is clear), and finally the delegate method is called.
Note that the begininvoke method can also be used in the delegate method.InvokeMethod, but when invoke is used, it will wait until the function call ends, and begininvoke will not wait until it goes forward directly.
Demo: Demo

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.