A small feature, which has already been implemented. I used it in a project. I think it is necessary to record it and write it down. Code
/* C #2005 and later do not support multi-thread direct access to interface controls (the interface creation thread is not the same as the access thread ),
* However, the following delegate-like solution can be used. */
Private Delegate Void Showmessage ( String Message );
Private Void Showfeedback ( String Feedback)
{
If ( ! This . Rtbmsg. invokerequired) // Directly operate controls when they are not accessed in the thread
{
This . Rtbmsg. Text + = Feedback;
}
Else // In-process access
{
Showmessage func = New Showmessage (showfeedback );
This . Begininvoke (func, New Object [] {FEEDBACK });
}
}
From the above, you may have seen how to operate on the same control through multiple threads, that is, through a delegate, and then define the delegate method to determine the control's invokerequired attribute (the metadata of this attribute is very well described, "because the call orientation is outside the thread where the control is created", it is clear), and finally the delegate method is called.
Note that the begininvoke method can also be used in the delegate method.InvokeMethod, but when invoke is used, it will wait until the function call ends, and begininvoke will not wait until it goes forward directly.
Demo: Demo