The invokerequired attribute of the control and the invoke method.

Source: Internet
Author: User
In the design, in order to separate the interface from the logic, my approach is to use the event. The interface only needs to respond to the event to process the display of the interface. Events may be triggered by different threads in logical processing. The response methods of these events will cause an exception when modifying the control content on the interface.

The control. invokerequired attribute and the invoke method are used.

Msdn says:
Gets a value that indicates whether the caller must call the invoke method when calling the control method, because the call orientation is in a thread other than the thread where the control is created.
If the handle of a control is created on a thread different from the call thread (you must call the control through the invoke method ), True Otherwise False .
Controls in Windows Forms are bound to specific threads and do not have thread security. Therefore, if you call the control method from another thread, you must use one invoke method of the control to mail the call to the appropriate thread. This attribute can be used to determine whether the invoke method must be called. It is useful when no thread has a control.

The following describes the usage (my general practice ):
First, define a delegate, which is the same as the signature of the event processing function. Of course, it is also possible to directly use the delegate of this event, such:

Private Delegate Void Invokecallback ( String MSG );
Then, determine the value of this attribute to determine whether to call 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;
}
}
Note: This function is an event processing function, and txtmessage is a text box.
In this way, the thread security of controls in the form is achieved.

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

Invokerequired: true when the current thread is not the thread where the control is created
For example, if you can open a thread by yourself or use timer events to access controls on the form, this attribute 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, it is new in thread.
If you call C. invokerequired in any method running in thread a, false is returned.
On the contrary, if any method running in thread B calls C. invokerequired, true is returned.
Whether the UI thread is irrelevant to the result. (Normally, the thread where the control is located is the UI thread, but there can be exceptions)

It can also be considered that when new control () is used, control records the current thread with a variable. When invokerequired is called, returns the thread recorded when the current thread is not new.

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

I understand: If invokerequired = true indicates that other threads need to access the control, the invoke is called to be transferred to the control owner for processing.

This article is from the csdn blog. For more information, see http: // Blog.csdn.net/hsgrass37301/archive/2009/10/02/4627166.aspx

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.