Thread (Parameterizedthreadstart) Initializes a new instance of the thread class, specifying a delegate that allows an 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, specifying a delegate that allows the object to be passed to the thread when it is started, and specifies the maximum stack size of 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 without parameters, we can use ThreadStart with a parameterizedthreadstart with a parameter. With a number of parameters in another way, described below.
One, without parameters of
usingSystem; usingSystem.Collections.Generic; usingSystem.Text; usingSystem.Threading; namespaceaaaaaa {classAAA { Public Static voidMain () {Thread T=NewThread (NewThreadStart (A)); T.start (); Console.read (); } Private Static voidA () {Console.WriteLine ("Method a!"); } } }
Results Show method A!
Two, with a parameter of the
Because Parameterizedthreadstart requires that the parameter type must be object, the defined method B parameter type must be object.
usingSystem; usingSystem.Collections.Generic; usingSystem.Text; usingSystem.Threading; namespaceaaaaaa {classAAA { Public Static voidMain () {Thread T=NewThread (NewParameterizedthreadstart (B)); T.start ("B"); Console.read (); } Private Static voidBObjectobj) {Console.WriteLine ("Method {0}!", obj. ToString ()); } } }
Results Show method B!
Three, with multiple parameters of the
Because thread provides only these two constructors by default, if you need to pass multiple arguments, we can use the parameters ourselves as properties of the class. This property is instantiated when the object of the class is defined and then manipulated.
usingSystem; usingSystem.Collections.Generic; usingSystem.Text; usingSystem.Threading; namespaceaaaaaa {classAAA { Public Static voidMain () {My m=NewMy (); M.x=2; M.y=3; Thread T=NewThread (NewThreadStart (M.C)); T.start (); Console.read (); } } classMy { Public intx, y; Public voidC () {Console.WriteLine ("X={0},y={1}", This. x, This. Y); } } }
Results show x=2,y=3
Four, using the structure to give the parameter value.
Define common public structs, which define the parameters you need, and then define an instance of the struct when you need to add a thread.
//Structural Body structRowcol { Public intRow; Public intCol; }; //Defining Methods Public voidOutput (Object rc) {rowcol rowcol=(rowcol) RC; for(inti =0; i < Rowcol.row; i++) { for(intj =0; J < Rowcol.col; J + +) Console.Write ("{0}", _char); Console.Write ("\ n"); } }
A summary of the method for thread transmission parameters of C # line