The difference between ThreadStart and Parameterizedthreadstart

Source: Internet
Author: User

ThreadStart:

ThreadStart This delegate is defined as void ThreadStart (), which means that the method being 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 define a ThreadStart type delegate that defines the method the thread needs to execute: Calculate, in this method, calculates the circumference of a circle with a diameter of 0.5 and outputs it. This constitutes the simplest example of multithreading, In many cases, that's enough.


Parameterthreadstart:

Parameterthreadstart is defined as void Parameterizedthreadstart (object state), the start function of the thread defined by this delegate can accept an input parameter, as follows:

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, and although there is only one argument, and it is of type object, a type conversion is still required, but it is good to have parameters, and by combining multiple parameters into a class and then passing an instance of the class as a parameter, You can implement multiple parameter passes. For example:

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 (a);
      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
}
}



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.