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