Original address: http://www.cnblogs.com/lovko/archive/2008/12/19/1358748.html
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.
usingSystem.Threading; Public Delegate voidMyinvoke (stringstr); Private voidBtnstartthread_click (Objectsender, EventArgs e) {Thread Thread=NewThread (NewThreadStart (Doword)); Thread. Start (); } Public voidDoword () {Myinvoke mi=NewMyinvoke (settxt); BeginInvoke (MI,New Object[]{"ABC"}); } Public voidSettxt (stringstr) {Txtreceive.text+ = str +System.Environment.NewLine; }
namespace is System.Windows.Forms
control. InvokeRequired 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 thread on which the control is created. true if the control ' s handle was created on a different thread than the calling thread (indicating this you Must make calls to the control through an Invoke method); Otherwise, false. "Data-guid=" 81d24962aa5a8f2436f95617bc477ade "> If the control's Handle is created on a different thread than the calling thread (stating that you must call the control through the Invoke method), true; otherwise false.
Other reference articles:
- Https://msdn.microsoft.com/zh-cn/library/system.windows.forms.control.invokerequired (v=vs.100). aspx
- Http://www.cnblogs.com/songxingzhu/p/3677307.html
- Http://www.cnblogs.com/poorboy/archive/2013/05/09/3069997.html
- Http://www.cnblogs.com/Rustle/articles/11301.aspx (Recommended reading)
- Http://www.cnblogs.com/marksun/archive/2012/02/29/2373383.html (Recommended reading)
- Http://www.cnblogs.com/whssunboy/archive/2007/06/07/775319.html (Recommended reading)
(ext) Use of the C # multithreaded Invoke method