Threads thread, parameters and parameters

Source: Internet
Author: User

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!"); }    }  }

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 ()); }    }  }

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); }    }  }
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); }    }  }

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 BodystructRowcol { 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"); }    }

Threads thread, parameters and parameters

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.