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