Methods for notifying the main thread after the C # child thread finishes _c# tutorial

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.