In multithreading, we often need to update the interface display in the working thread, but an error will be reported when the interface control method is directly called in multithreading, invoke and begininvoke occur to solve this problem, so that you can safely update the interface display in multiple threads.
The correct method is to encapsulate the code that involves updating the interface in the working thread as a method and call it through invoke or begininvoke. The difference between the two is that one causes the worker thread to wait, while the other does not.
The so-called "one-side response operation, one-side node addition" can always be relative, so that the burden on the UI thread is not too great, because correct interface updates must always be done through the UI thread, what we need to do is to wrap most of the operations in the work thread, and put the pure interface updates into the UI thread for the purpose of reducing the burden on the UI thread.
For example, when you start a thread and want to update a textbox in the form in the thread method ..
Using system. Threading;
// Start a thread
Thread thread = new thread (New threadstart (dowork ));
Thread. Start ();
// Thread method
Private void dowork ()
{
This. textbox1.text = "I Am a text box ";
}
If you operate like above, there will be exceptions in vs2005 or 2008...
The correct method is to use invoke \ begininvoke
Using system. Threading;
Namespace Test
{
Public partial class form1: Form
{
Public Delegate void myinvoke (string str1, string str2 );
Public form1 ()
{
Initializecomponent ();
}
Public void dowork ()
{
Myinvoke MI = new myinvoke (updateform );
This. begininvoke (MI, new object [] {"I Am a text box", "Haha "});
}
Public void updateform (string param1, string parm2)
{
This. textbox1.text = param1 + parm2;
}
Private void button#click (Object sender, eventargs E)
{
Thread thread = new thread (New threadstart (dowork ));
Thread. Start ();
}
}
}
In multithreading, we often need to update the interface display in the working thread, but an error will be reported when the interface control method is directly called in multithreading, invoke and begininvoke occur to solve this problem, so that you can safely update the interface display in multiple threads.
The correct method is to encapsulate the code that involves updating the interface in the working thread as a method and call it through invoke or begininvoke. The difference between the two is that one causes the worker thread to wait, while the other does not.
The so-called "one-side response operation, one-side node addition" can always be relative, so that the burden on the UI thread is not too great, because correct interface updates must always be done through the UI thread, what we need to do is to wrap most of the operations in the work thread, and put the pure interface updates into the UI thread for the purpose of reducing the burden on the UI thread.
For example, when you start a thread and want to update a textbox in the form in the thread method ..
Using system. Threading;
// Start a thread
Thread thread = new thread (New threadstart (dowork ));
Thread. Start ();
// Thread method
Private void dowork ()
{
This. textbox1.text = "I Am a text box ";
}
If you operate like above, there will be exceptions in vs2005 or 2008...
The correct method is to use invoke \ begininvoke
Using system. Threading;
Namespace Test
{
Public partial class form1: Form
{
Public Delegate void myinvoke (string str1, string str2 );
Public form1 ()
{
Initializecomponent ();
}
Public void dowork ()
{
Myinvoke MI = new myinvoke (updateform );
This. begininvoke (MI, new object [] {"I Am a text box", "Haha "});
}
Public void updateform (string param1, string parm2)
{
This. textbox1.text = param1 + parm2;
}
Private void button#click (Object sender, eventargs E)
{
Thread thread = new thread (New threadstart (dowork ));
Thread. Start ();
}
}
}