C # simple cross-thread assignment

Source: Internet
Author: User

Generally, C # is not allowed to assign values across threads. For example, there is a textbox in form1. If a new thread is opened

An exception occurs when the textbox value is assigned.

The specific practices are put forward in msdn, which is complicated and will not be mentioned.

Here is a simple method.

Most controls that can be assigned values have the invoke method. Isn't a delegate parameter required in the invode method,

That's simple. You can find a. Net custom delegate, such as threadstart. Therefore, you can do this:

Yourcontrol. Invoke (New threadstart (delegate (){
Yourcontrol. Text =Str; =Note: This STR is the variable in the form1 main thread.
}));

Simply put, you don't need to define the delegate, or write additional methods or other complicated things.

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.