In multithreaded programming, we often have to update the interface display in the worker thread, and the method of invoking the interface control directly in multi-threading is the wrong way, and invoke and BeginInvoke are to solve this problem, which makes you display in a secure update interface in multi-thread.
The correct approach is to encapsulate the code that involves the update interface in a worker thread as a method, called by Invoke or BeginInvoke, and the difference is that one causes the worker to wait, while the other does not.
The so-called "side response operation, one side add node" can always be relative, so the UI thread burden is not too large, because the correct interface is always to be updated through the UI thread to do, we have to do is to take the majority of operations in the worker thread, and will be the pure interface updates into the UI thread to do, This also achieves the purpose of easing the burden on the UI thread.
Give a simple example of how to use it, such as when you start a thread, and you want to update a textbox in the form in the method of the threads.
Using System.Threading;
public delegate void Myinvoke (String str);
private void Btnstartthread_click (object sender, EventArgs e)
{
Thread thread = new Thread (new ThreadStart (Doword));
Thread. Start ();
}
public void Doword ()
{
Myinvoke mi = new Myinvoke (settxt);
BeginInvoke (mi,new object[]{"abc"});
}
public void Settxt (String str)
{
Txtreceive.text + = str + System.Environment.NewLine;
}
Use of the C # multithreaded Invoke method