//The methods and parameters executed by the thread are encapsulated in a class. By instantiating the class, the method can invoke the property to implement the indirect type to pass the parameter safely.
UsingSystem;usingSystem.Threading;//The Threadwithstate class contains the tasks to be performed and the methods to perform the tasks . Public classThreadwithstate {//the property to use, which is the argument we're going to pass. Private stringboilerplate; Private intvalue; //constructors that contain parameters PublicThreadwithstate (stringTextintNumber ) {Boilerplate=text; Value=Number ; } //to throw to the thread execution method, there is no return type in order to allow ThreadStart to invoke Public voidThreadProc () {//This is the task to be performed, and we will show only the parameters passed inConsole.WriteLine (boilerplate, value); }}//The class used to invoke the above method, which is the entry for this example Public classExample { Public Static voidMain () {//instantiate the Threadwithstate class to provide parameters for the threadThreadwithstate TWS =NewThreadwithstate ("This report displays the number {0}.", the); //creates the thread that performs the task and executes theThread T =NewThread (NewThreadStart (TWS). ThreadProc)); T.start (); Console.WriteLine ("Main thread does some work and then waits."); T.join (); Console.WriteLine ("independent task has completed; main thread ends."); }}
C # Multithreading methods can be used to pass parameters