A summary of the method for thread transmission parameters of C # line

Source: Internet
Author: User
Tags compact thread class

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

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.