Preface
As mentioned in the previous section, the parameter for creating a thread is a function. When a new thread starts, it will execute this function without any parameters, if there is no return value, how can we call a function with parameters?
Reading directory
I. Passing Parameters Using Constructors
Ii. Operating Mechanism
Iii. Running Effect
I. Passing Parameters Using Constructors
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Threading;
Namespace _ 2_ThreadArgs
{
Class Program
{
Static void Main (string [] args)
{
// Parameters passed by the constructor
User user = new User ("getting sleepy since childhood ");
Thread thread = new Thread (new ThreadStart (user. GetName ));
Thread. Start ();
Thread. Join (Timeout. Infinite );
}
}
/// <Summary>
/// User class
/// </Summary>
Class User
{
Private string _ name;
Public User (string strName)
{
_ Name = strName;
}
Public void GetName ()
{
Console. WriteLine ("My name is:" + _ name );
}
}
}
Ii. Operating Mechanism
First, we construct a class User with parameters and input a string. Then, the class is saved in a class member variable through the constructor, and there is also a GetName () the method is to output a sentence. First, we construct a class, and then use the GetName () method of this class as the ThreadStart () parameter, that is, using the constructor to pass the Parameter
Iii. Running Effect