Differences between parameterizedthreadstart and threadstart in C #

Source: Internet
Author: User
    • Threadstart does not need to pass parameters or return parameters.

We know that the most intuitive way to start a thread is to use the Thread class. The specific steps are as follows:

InstanceCode:

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

 

We have definedThreadstart type DelegationThis delegate develops the method to be executed by the thread: Calculate, in which the area of a circle with a diameter of 0.5 is calculated and output. this constitutes the simplest example of multithreading. In many cases, this is enough, and the threadstart delegate is defined as void threadstart (), that is, the method to be executed does not have parameters. This is obviously a big deficiency. To make up for this defect, it is smart.ProgramThe employee came up with many good methods. We will introduce them in the section that needs to pass multiple parameters. Here we will introduce them first. net another delegate set to solve this problem: parameterizedthreadstart, which will be detailed below.

    • parameterizedthreadstart A single parameter needs to be passed

      parameterthreadstart is defined as void parameterizedthreadstart (Object state) the startup function of the thread defined by this delegate can accept an input parameter. The example is as follows

      instance code:

      parameterizedthreadstart threadstart = new parameterizedthreadstart (calculate)
      thread = new thread ()
      thread. start (0.9);
      Public void calculate (Object Arg)
      {< br> 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 the object type. Although there is only one parameter, it is still of the object type, type conversion is still required for use, but it is good to have parameters. By combining multiple parameters into a class, the instance of this class is passed as a parameter, multiple parameters can be passed

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.