This article is a mirror article of machine translation, please click here to jump to the original article.

View: 19083|Reply: 0

[Source] ThreadStart and ParameterizedThreadStart are different

[Copy link]
Posted on 11/14/2016 10:25:44 AM | | |
ThreadStart:

The ThreadStart delegate is defined as void ThreadStart(), which means that the method executed cannot have parameters.
ThreadStart threadStart=new ThreadStart(Calculate);
Thread thread=new Thread(threadStart);
thread. Start();
public void Calculate()
   {
double Diameter=0.5;
Console.Write("The Area Of Circle with a Diameter of {0} is {1}"Diameter,Diameter*Math.PI);
   }
       Here we use a delegate that defines a ThreadStart type, which defines the method that the thread needs to execute: Calculate, in which the circumference of a circle with a diameter of 0.5 is calculated, and outputs. This constitutes the simplest example of multithreading, which in many cases is sufficient




ParameterThreadStart:
ParameterThreadStart is defined as void ParameterizedThreadStart(object state), and the startup function of the thread defined using this delegate can accept an input parameter, for example:



ParameterizedThreadStart threadStart=new ParameterizedThreadStart(Calculate)
Thread thread=new Thread() ;
thread. Start(0.9);
public void Calculate(object arg)
{
double Diameter=double(arg);
Console.Write("The Area Of Circle with a Diameter of {0} is {1}"Diameter,Diameter*Math.PI);
}

The Calculate method has a parameter of type object, although there is only one parameter, and it is also an object type, and it still needs to be converted when using it, but fortunately, there can be parameters, and by combining multiple parameters into a class, and then passing the instance of this class as a parameter, you can achieve multiple parameter transfer. Like what:





class AddParams
{
    public int a, b;

    public AddParams(int numb1, int numb2)
    {
      a = numb1;
      b = numb2;
    }
}
#endregion

class Program
{
    static void Main(string[] args)
    {
      Console.WriteLine("***** Adding with Thread objects *****");
      Console.WriteLine("ID of thread in Main(): {0}",
        Thread.CurrentThread.ManagedThreadId);

      AddParams ap = new AddParams(10, 10);
      Thread t = new Thread(new ParameterizedThreadStart(Add));
      t.Start(ap);
      Console.ReadLine();
    }

    #region Add method
    static void Add(object data)
    {
      if (data is AddParams)
      {
        Console.WriteLine("ID of thread in Main(): {0}",
          Thread.CurrentThread.ManagedThreadId);

        AddParams ap = (AddParams)data;
        Console.WriteLine("{0} + {1} is {2}",
          ap.a, ap.b, ap.a + ap.b);
      }
    }
    #endregion
}
}




Previous:.net/c# Crawl user avatars, grab QQ avatars
Next:.net/c# calculates the age of the driver's car
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com