In fact, this is relatively simple, child threads how to inform the main thread, is to let the child thread to do their own thing to do the main thread back to do the main thread of the thing.
So how to let the child thread to do the main course of things, we just need to pass the main thread of the method to the child threads on the line, then the transfer method is very simple delegate value;
Here's an example of a child thread doing one thing and having done the notification main thread
The public class program
{
//defines a delegate public
delegate void entrust (String str);
static void Main (string[] args)
{
Entrust callback = new Entrust (callback);//Assign the method to the delegate
thread th = new Thread ( Fun);
Th. IsBackground = true;
Th. Start (callback);//passes the delegate to the child thread
console.readkey ();
}
private static void Fun (Object obj) {
//NOTE: The parameter of the thread is type object for
(int i = 1; I <= i++)
{
CONSOLE.W Riteline ("sub-thread loop operation {0} times", i);
Thread.Sleep (+);
}
Entrust callback = obj as entrust;//strong to delegate
callback ("I am a child thread, I finished, notify the main thread");
The loop of the child thread executes the method of the main thread the method
}
//main thread
private static void CallBack (String str) {
Console.WriteLine ( STR);
}
Above is a process by which a delegate Cheng a value (that is, the main thread) to the main line, above which we define a delegate ourselves, which we can use, of course. NET is handled by the action<> and fun<> generic delegates we provide, just like this
The public class program
{
//defines a delegate public
delegate void entrust (String str);
static void Main (string[] args)
{
action<string> callback = ((String str) => {Console.WriteLine (str);} );
Lamuda expression
Thread th = new Thread (Fun);
Th. IsBackground = true;
Th. Start (callback);
Console.readkey ();
}
private static void Fun (Object obj) {for
(int i = 1; I <= i++)
{
Console.WriteLine ("Child Thread loop operation {0} times" , i);
Thread.Sleep (+);
}
Action<string> callback = obj as action<string>;
Callback ("I am a child thread, I am finished, notify the main thread")
;
}
The Lamuda expression above can also return to the back of the anonymous function
//action<string> callback = delegate (String str) {Console.WriteLine (str);
Here is the result of the run
Above this C # child thread to notify the main thread after the method is small to share all the content of everyone, hope to give you a reference, but also hope that we support the cloud habitat community.