How to pass parameters to a thread

Source: Internet
Author: User

NET provides a lot of multithreaded programming tools, probably because too many, so there are always some headaches, I am here to talk about my summary of some of the experience of multithreaded programming, I hope to help everyone

Does not need to pass parameters, nor does it need to return parameters
We know that the most intuitive way to start a thread is to use the thread class, as follows:

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

}

Many people know how to open a thread, but do not know how to properly pass parameters to the thread.

The common mistake is to use global variables to pass parameters to the thread, in fact, the disadvantage of doing this is not secure, two is more troublesome, and three is if you want to open more than one thread, it is more troublesome.

Above we define a ThreadStart type of delegate that defines the method that the thread needs to execute: Calculate, in this method, calculates the area of a circle with a diameter of 0.5, and outputs it. This makes up the simplest example of multithreading, which in many cases is sufficient, Then threadstart this delegate is defined as void ThreadStart (), that is, the method executed cannot have parameters, which is obviously a big disadvantage, in order to compensate for this flaw, smart programmers come up with a lot of good methods, we will be in the need to pass multiple parameters in the section of the introduction , here we introduce first. NET in order to solve this problem and set up another delegate: is Parameterizedthreadstart, I will explain in detail below

Need to pass a single argument

Parameterthreadstart is defined as void Parameterizedthreadstart (object state)?? You can accept an input parameter using the startup function of the thread defined by this delegate, as in the following example

Parameterizedthreadstart threadstart=new Parameterizedthreadstart (Calculate)

Thread Thread=new thread ()

Thread. Start (0.9);


It is important to note that the variables here must be of type object, and if you need to pass the interface (such as using an interface parameter for Arcengine development), you need to first convert to object, when used, in the transition back
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 argument, and it is an object type, it is necessary to use the type conversion, but it is good to have parameters, and by combining multiple parameters into a class, and then passing the instance of the class as a parameter, You can implement multiple parameter passing.

The right thing to do is to encapsulate the parameters required by the thread and the thread entry functions separately into a class, and encapsulate them if you need some auxiliary functions. This code is simple, clear, and easy to reuse.

Then use the example of a thread class to specify, first of all, the thread class:

--------------------------Threadfun Class------------------------------------

//-----------------------------------------------------------------------------

---File:clsThreadFun.cs

---Description:this class demonstrates how to use the thread class.

---author:knight

---date:mar.21, 2006

//-----------------------------------------------------------------------------

------------------------{threadfun Class}----------------------------------

Using System;

Namespace Csnewtest

{

<summary>

Summary description for Clsthreadfun.

</summary>

public class Clsthreadfun

{

private string strUserName;

public string UserName

{

get{return strUserName;}

set{strUserName = value;}

}

Public Clsthreadfun (String susername)

{

//

Todo:add constructor Logic here

//

strUserName = sUserName;

}

<summary>

Thread interface function

</summary>

public void Threadfun ()

{

Detail thread Handle

}

}

}

Next, is the calling code:

Clsthreadfun mythreadfun = new Clsthreadfun ("Test");

Set parameter through "Mythreadfun.username"

Thread myThread = new Thread (new ThreadStart (Mythreadfun.threadfun));

Mythread.start ();

How to pass parameters to a thread

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.