C # Passing parameters to the thread using an anonymous method (you can pass any parameter without unpacking or packing) [reprint]

Source: Internet
Author: User
C # Passing parameters to the thread using an anonymous method (you can pass any parameter without unpacking or packing)

C # Passing parameters to the thread using an anonymous method (you can pass any parameter without unpacking or packing)

Pass data to threadstart

In the above example, we want to better distinguish the output results of each thread and let one thread output uppercase letters. We input a state word to go to complete the entire task, but we cannot use threadstart to delegate because it does not accept the parameter. Fortunately ,. net Framework defines another version of the delegate called parameterizedthreadstart, which can receive a separate object type parameter:

public delegate void ParameterizedThreadStart (object obj);

The previous example looks like this:

class ThreadTest {
  static void Main() {
    Thread t = new Thread (Go);
    t.Start (true);             // == Go (true) 
    Go (false);
  }
  static void Go (object upperCase) {
    bool upper = (bool) upperCase;
    Console.WriteLine (upper ? "HELLO!" : "hello!");
  }

Hello!
Hello!

In the entire example, the compiler automatically deduce the parameterizedthreadstart delegate, because the go method receives a separate object parameter and writes it like this:

Thread t = new Thread (new ParameterizedThreadStart (Go));
t.Start (true);

Parameterizedthreadstart is a feature that requires packing of the desired type (bool here) before use, and it can only receive one parameter.

An alternative solution is to use an anonymous method to call a common method as follows:

static void Main() {
  Thread t = new Thread (delegate() { WriteText ("Hello"); });
  t.Start();
}
static void WriteText (string text) { Console.WriteLine (text); }

The advantage is that the target method (writetext here) can receive any number of parameters without packing. However, you need to put an external variable into the anonymous method, as shown below:

static void Main() {
  string text = "Before";
  Thread t = new Thread (delegate() { WriteText (text); });
  text = "After";
  t.Start();
}
static void WriteText (string text) { Console.WriteLine (text); }

After

 

The anonymous method opens up a strange phenomenon. When an external variable is modified by a later part of the variable, it may be inadvertently interacted through the external variable. Intentional interactions (usually through fields) are considered sufficient! Once the thread starts running, it is recommended that the external variables be processed as read-only-unless someone is willing to use the appropriate lock.

Another common method is to pass the object instance method instead of the static method into the thread. The attributes of the object instance can tell the thread what to do, as shown in the following example:

class ThreadTest {
  bool upper;
 
  static void Main() {
    ThreadTest instance1 = new ThreadTest();
    instance1.upper = true;
    Thread t = new Thread (instance1.Go);
    t.Start();
    ThreadTest instance2 = new ThreadTest();
Instance2.go (); // main thread -- run upper = false
  }
 
  void Go() { Console.WriteLine (upper ? "HELLO!" : "hello!"); }

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.