C # Methods for passing parameters to threads summary _c# tutorial

Source: Internet
Author: User

There are two ways to pass a parameter:

1, to create the parameters of the constructor method class parameters

2. Using Thread.Start (8) to pass the parameter directly, the method receives an object and passes the object to the thread, so the method that starts in the thread

You must receive a single parameter of type object.

Thread (Parameterizedthreadstart) Initializes a new instance of the thread class, specifying a delegate to allow the object to be passed to the thread when it is started.

Thread (ThreadStart) Initializes a new instance of the thread class.

Supported by the. NET Compact Framework.

Thread (Parameterizedthreadstart, Int32) Initializes a new instance of the thread class, specifies the delegate that is allowed to be passed to the thread when the thread starts, and specifies the maximum stack size for the thread.

Thread (ThreadStart, Int32) Initializes a new instance of the thread class, specifying the maximum stack size of the threads.

Supported by the. NET Compact Framework.

If we define a thread with no parameters, we can use the ThreadStart with a parameterizedthreadstart with a parameter. Use a different method with multiple parameters, as described below.

One, without parameters of

Using System; 
Using System.Collections.Generic; 
Using System.Text; 
Using System.Threading; 
Namespace Aaaaaa 
{ 
class AAA 
{public 
static void Main () 
{ 
thread t = new Thread (new ThreadStart (A)); 
T.start (); 
Console.read (); 
} 
private static void A () 
{ 
Console.WriteLine ("Method a!"); 
} 
 

The results Show method A!

Two, with a parameter of the

Because the Parameterizedthreadstart requires that the parameter type must be object, the defined method B parameter type must be object.

Using System; 
Using System.Collections.Generic; 
Using System.Text; 
Using System.Threading; 
Namespace Aaaaaa 
{ 
class AAA 
{public 
static void Main () 
{ 
thread t = new Thread (new Parameterizedthreadstart (B)); 
T.start ("B"); 
Console.read (); 
} 
private static void B (Object obj) 
{ 
Console.WriteLine ("Method {0}!", obj.) ToString ()); 
} 
 

The results Show method B!

Three, with multiple parameters of the

Since thread defaults to providing only these two constructors, if you need to pass more than one argument, we can use the parameter as the property of the class ourselves. Instantiate the property when defining the object of the class, and then do the operation.

Using System; 
Using System.Collections.Generic; 
Using System.Text; 
Using System.Threading; 
Namespace Aaaaaa 
{ 
class AAA 
{public 
static void Main () 
{me 
m = new My (); 
m.x = 2; 
M.Y = 3; 
Thread t = new Thread (new ThreadStart (M.C)); 
T.start (); 
Console.read (); 
} 
Class I 
{public 
int x, y; 
public void C () 
{ 
Console.WriteLine ("X={0},y={1}", This.x, This.y); 
} 
 

Results show x=2,y=3

Four, use the structure body to pass the value to the parameter.

Defines the public struct that you can define the parameters you need, and then, when you need to add a thread, you can define an instance of the struct body.

The structure body 
struct rowcol 
{public 
int row; 
public int col; 
}; 
Define method public 
void Output (Object rc) 
{ 
Rowcol rowcol = (rowcol) RC; 
for (int i = 0; i < Rowcol.row. i++) 
{ 
for (int j = 0; J < Rowcol.col; j +) 
Console.Write ("{0}", _ch AR); 
Console.Write ("\ n"); 
} 

The above is a small set to introduce C # to the thread to pass the parameters of the method summary, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.