What is the begininvoke dance.
Delegate void myhandlerdelegate ();
Void myhandler ()
{
// "The begininvoke dance"
If (this. invokerequired ){
Begininvoke (New myhandlerdelegate (myhandler ));
Return;
}
Do GUI stuff
}
When developing multi-threaded programs, we often need to feedback the results to the UI after a large amount of computing. if you update the UI directly when multithreading occurs, an exception is thrown. I believe everyone has encountered such a problem, and the solution is the above so-called begininvoke dance.
Asyncoperationmanager is provided in 2.0. classes that support asynchronous method calls provide concurrent management. It can be used to avoid begininvoke. Besides, it is annoying to write such a stuff every time.
Public partial class form1: Form
{
Asyncoperation;
Public form1 ()
{
Initializecomponent ();
}
Private void button#click (Object sender, eventargs E)
{
Asyncoperation = asyncoperationmanager. createoperation (null );
Thread t = new thread (New threadstart (runtask ));
T. Start ();
}
Public void runtask ()
{
Thread. Sleep (5000 );
Asyncoperation. Post (New sendorpostcallback (showtext), "hello ");
}
Void showtext (Object S)
{
This. textbox1.text = S. tostring ();
}
}