Use of Invoke in C #

Source: Internet
Author: User

When building the GUI program interface with the. NET Framework WinForm, if you want to change the state of the control in the control's event response function, for example: The text on a button is originally called "Open" and the text on the button after the click shows "Off", beginners tend to assume that:

void Buttononclick (Object Sender,eventargs e)

{

button. Text= "Off";

}

After you run a program like this, an exception may be triggered, and the exception information is roughly "cannot be called from a thread that is not creating the control." Note that this is "possible" and does not necessarily trigger this type of exception. This exception is caused by the fact that the control is created in the main thread (such as this. Controls.Add (...);), when entering the control's event response function, is the thread in which the control is located, not the main path. Changing the state of the control in the event response function of the control may have a thread conflict with the main threads. If the main thread is redrawing the appearance of the control, it can cause confusion when other threads change the appearance of the control. However, such a situation does not always occur, if the main thread at this time in the redrawing of the other controls, it may escape a robbery, such a way to pass the normal, no triggering exception.

The correct way to do this is to invoke the control's Invoke method in the control's response function (in fact, if you have used C + + builder before, you will find a function like invoke that activates to the main thread). The Invoke method searches up the control tree until it finds the thread that created the control (usually the main thread), and then enters that thread to change the appearance of the control, ensuring that no thread conflicts occur. Examples of correct wording are as follows:

void Buttononclick (Object Sender,eventargs e)

{

button. Invoke (New EventHandler (delegate

{

button. Text= "Off";

}));

}

The Invoke method needs to create a delegate. You can write the functions and the corresponding delegates beforehand. However, the example code above is a good choice if you want to visually see a specific function when invoking the Invoke method, rather than searching elsewhere.

There is a disturbing way to do this: Different controls are written differently. For the textbox, to Textboxobject.invoke, for the label, but also Labelobject.invoke. Is there a unifying point?

The main window class itself also has an invoke method. If you don't want to have different controls written differently, you can use this all. Invoke:

void Buttononclick (Object Sender,eventargs e)

{

This. Invoke (New EventHandler (delegate

{

button. Text= "Off";

}));

}

In C # 3.0 and later versions, there are LAMDA expressions, such as the above anonymous delegate has a more concise wording. The. NET Framework 3.5 and later versions are more capable of using the action wrapper method. For example, the following wording can look very concise:

void Buttononclick (Object Sender,eventargs e)

{

This. Invoke (new Action () =

{

button. Text= "Off";

}));

}

The above writing is often filled with winform built programs.

In the new generation of Microsoft Interface Development technology in WPF, because interface rendering and business logic are ecologically separate in two threads, the control's event response function does not have to invoke. However, if you manually open a new thread, you will still invoke it if you change the appearance of the control in this new thread.

Use of Invoke in C #

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.