InvokeRequired and Invoke

Source: Internet
Author: User

C#中禁止跨线程直接访问控件, InvokeRequired is created to solve this problem, when a control's InvokeRequired property value is true, it is stated that a thread other than the one creating it wants to access it . At this point it will call the new MethodInvoker (Loadglobalimage) internally to complete the following steps, which ensures the security of the control, you can understand that some people want to borrow money from you, he can directly in your wallet, so it is not safe, So you have to let someone else tell you first, you take the money out of your wallet and lend it to someone else, it's safe.

------------------------------------------------------------------------

In order for the interface to be separated from the logic in the design, my approach is to use events, and the interface simply responds to events to handle the display of the interface. Events can be raised in logical processing by different threads, and the response method of these events throws an exception when modifying the contents of the control in the interface.

In this case, the control.invokerequired attribute and the Invoke method are used.

------------------------------------------------------------------------

Gets a value that indicates whether the caller must call the Invoke method when making a method call to the control, because the call is in a thread other than the one on which the control is created.

True if the control's Handle is created on a different thread than the calling thread (indicating that you must call the control through the Invoke method);

Controls in Windows forms are bound to a specific thread and do not have thread safety. Therefore, if you invoke a control's method from another thread, you must use an Invoke method of the control to marshal the call to the appropriate thread. This property can be used to determine whether the Invoke method must be called, which is useful when you do not know what thread owns the control.

------------------------------------------------------------------------

A delegate is defined first, as is the signature of the event handler, and of course the delegate that uses the event directly is also possible, such as:

Private delegate void Invokecallback (string msg);

The value of this property is then judged to determine whether to invoke the Invoke function:

void M_comm_messageevent (String msg)

{

if (txtmessage.invokerequired)

{

Invokecallbackmsgcallback = new Invokecallback (m_comm_messageevent);

Txtmessage.invoke (Msgcallback, new Object [] {msg});

}

Else

{

Txtmessage.text = msg;

}

}

Description: This function is the event handler, Txtmessage is a text box.

This allows for thread safety of the controls in the form.

------------------------------------------------------------------------

InvokeRequired true if the current thread is not the thread that created the control

For example, if you can open a thread yourself, or use the timer event to access a control on a form, this property of the form in the thread is true.

Simply put, if there are two threads, thread A and thread B, and there is a control C, which is new in thread A.

Then any method call c.invokerequired running in thread A will return false.

Conversely, if any method call c.invokerequired that is running inside thread B, it will return true.

Whether the UI thread is unrelated to the result. (The thread that the control is usually on is the UI thread, but there can be exceptions)

It can also be thought that at the time of new control (), control uses a variable to record the current thread, and when calling InvokeRequired, returns the thread that is logged when the current thread is not equal to new.

InvokeRequired and Invoke

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.