C # Multithreading methods can be used to pass parameters

Source: Internet
Author: User

//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

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.