1. Thread
C # in the program, if you want to create a new thread, you usually need to create another method. If you want to pass parameters, the steps are troublesome,
Reference 1
Thread othread = new thread (New threadstart (threadmethod ));
Private void threadmethod (){
..
};
If you want to pass parameters to the thread
Public static void mystaticparamthreadmethod (Object OBJ ){
Console. writeline (OBJ );
}
Thread thread = new thread (mystaticparamthreadmethod );
Thread. Start (OBJ );
Of course, there are other writing methods. For more information, see reference 1.
However, if you use an anonymous class, it will be simpler.
Thread othread = new thread (delegate ()
{
....
});
Othread. Start ();
2. Control. Invoke
In C #, if you call multiple threads to update the UI attributes, an exception is reported: Cross-thread operation not valid.
The general solution is to declare a delegate and call it through the delegate. For details, refer to 2.
Private delegate void flushclient (); // proxy
Private void threadfunction ()
{
If (textbox1.invokerequired) // wait for Asynchronization
{
Flushclient fc = new flushclient (threadfunction );
This. Invoke (FC); // call the refresh method through the proxy
Return;
}
Textbox1.text = datetime. Now. tostring ();
}
Here, we need to declare a delegate first, which is quite troublesome, because we always need to write more delegates, but these delegates are a temporary transition. After being changed to an anonymous delegate, we do not need to declare those delegates.
Private void threadfunction ()
{
If (textbox1.invokerequired) // wait for Asynchronization
{
This. Invoke (New methodinvoker (delegate ()
{
Threadfunction ();
}));
Return;
}
Textbox1.text = datetime. Now. tostring ();
}
The amount of code is not reduced, but you do not have to declare the temporary delegate.
Reference 1: http://developer.51cto.com/art/200908/141590.htm
Reference 2: http://www.cnblogs.com/zhaotiantang/archive/2009/03/17/1414135.html