C # multi-threaded call with parameter functions

Source: Internet
Author: User

[Html]
Thread operations mainly use the Thread class, which is defined under System. Threading. dll. This reference must be added for use. This class provides four overloaded Constructors (the following is derived from msdn ).

Thread (ParameterizedThreadStart) initializes a new instance of the Thread class and specifies the delegate that allows the object to be passed to the Thread when the Thread starts.
Thread (ThreadStart) initializes a new instance of the Thread class.
Supported by. NET Compact Framework.

Thread (ParameterizedThreadStart, Int32) initializes a new instance of the Thread class, specifies the delegate that allows the object to be passed to the Thread when the Thread starts, and specifies the maximum stack size of the Thread.
Thread (ThreadStart, Int32) initializes a new instance of the Thread class and specifies the maximum stack size of the Thread.
Supported by. NET Compact Framework.



If we define a thread without parameters, we can use ThreadStart and ParameterizedThreadStart with a parameter. The following describes how to use other methods with multiple parameters.

1. Without Parameters

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 ());
T. Start ();

Console. Read ();
}

Private static void ()
{
Console. WriteLine ("Method! ");
}
}
}


The result shows Method!

2. With a parameter

Because ParameterizedThreadStart requires that the parameter type be object, the type of the parameter defined in Method B 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 result shows Method B!

3. Parameters

Because Thread only provides the two constructors by default, if you need to pass multiple parameters, we can use the parameters as the attributes of the class. Instantiate this attribute when defining the class object, and then perform operations.

Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. Threading;

Namespace AAAAAA
{
Class AAA
{
Public static void Main ()
{
My m = new My ();
M. x = 2;
M. y = 3;

Thread t = new Thread (new ThreadStart (m. C ));
T. Start ();

Console. Read ();
}
}

Class My
{
Public int x, y;

Public void C ()
{
Console. WriteLine ("x = {0}, y = {1}", this. x, this. y );
}
}
}


The result shows x = 2, y = 3.

4. Use struct to pass values to parameters.

Define public struct, which can define the parameters you need, and then define the instance of the struct when you need to add a thread.

// Struct
Struct RowCol
{
Public int row;
Public int col;
};

// Define the 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}", _ char );
Console. Write ("\ n ");
}
}

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.