Differences between ParameterizedThreadStart and ThreadStart in C #

Source: Internet
Author: User

You do 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:

ThreadStart threadStart = new ThreadStart (Calculate );
ThreadThread = newThread (threadStart );
Thread. Start ();

 


PublicVoidCalculate ()

{
DoubleDiameter = 0.5;
Console. Write ("AreaOfCircleWithADiameterOf{0}Is{1} "Diameter, Diameter * Math. PI );
}

We have definedThreadStart type DelegationThis delegate develops the method to be executed by the thread: Calculate. In this method, calculates the circumference of a circle with a diameter of 0.5 and outputs the result. 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 huge deficiency. To make up for this defect, a smart programmer has come up with many good methods, we will introduce it in the section that needs to pass multiple parameters. Here we will first introduce it. net another delegate set to solve this problem: ParameterizedThreadStart.

  A single parameter needs to be passed

ParameterThreadStart is defined as void.ParameterizedThreadStart (objectState )?? The startup function of the thread defined by this delegate can accept an input parameter. The example is as follows:
ParameterizedThreadStart ThreadStart = newParameterizedThreadStart (Calculate)
ThreadThread = newThread ();

Thread. Start (0.9 );

 


PublicVoidCalculate (objectArg)

{
DoubleDiameter = double (arg );
Console. Write ("AreaOfCircleWithADiameterOf{0}Is{1} "Diameter, Diameter * Math. PI );
}

The Calculate method has a parameter of the object type. Although there is only one parameter and it is of the object type, type conversion is still required when using it, but fortunately there is a parameter, by combining multiple parameters into a class, and passing the instance of the class as a parameter, multiple parameters can be passed. 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 (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
}
}

 

 

Related Article

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.