How parameters are passed between different threads in the same application domain in C #

Source: Internet
Author: User

It's been a long time since I wrote a blog, the latest project doesn't have to write code. It's fine today. The method of parameter passing between threads, which is mainly used for parameter passing between two methods running on a different thread . Look directly at the code

1. Passing parameters directly between methods

   voidDemoparam () {Console.WriteLine ("Demoparam:"+Thread.CurrentThread.ManagedThreadId); //thread t = new Thread (new Parameterizedthreadstart (Testparam)); //T.start ("majaing");ThreadPool.QueueUserWorkItem (NewWaitCallback (Testparam),"majaing"); }        voidTestparam (Objectobj) {Console.WriteLine ("Demoparam:"+Thread.CurrentThread.ManagedThreadId); Console.WriteLine (obj.        ToString ()); }

2. With static

 //[ThreadStatic]        Static stringNamekey; voiddemostatic () {Console.WriteLine ("Static:"+Thread.CurrentThread.ManagedThreadId); Namekey="Majiang"; ThreadPool.QueueUserWorkItem (NewWaitCallback (teststatic)); }        voidTeststatic (Objectobj) {Console.WriteLine ("Static:"+Thread.CurrentThread.ManagedThreadId);        Console.WriteLine (Namekey); }

3. With the AppDomain

 voidDemoappdomain () {Console.WriteLine ("AppDomain:"+Thread.CurrentThread.ManagedThreadId); AppDomain.CurrentDomain.SetData ("name","Majiang"); ThreadPool.QueueUserWorkItem (NewWaitCallback (Testappdomain)); }        voidTestappdomain (Objectobj) {Console.WriteLine ("AppDomain:"+Thread.CurrentThread.ManagedThreadId); varA = AppDomain.CurrentDomain.GetData ("name");        Console.WriteLine (a); }

4. With CallContext

  voidDemocallcontext () {Console.WriteLine ("CallContext"+Thread.CurrentThread.ManagedThreadId); //Executioncontext.suppressflow ();Callcontext.logicalsetdata ("name","Majiang"); ThreadPool.QueueUserWorkItem (NewWaitCallback (Testcallcontext)); }        voidTestcallcontext (Objectobj) {Console.WriteLine ("CallContext"+Thread.CurrentThread.ManagedThreadId); varA = Callcontext.logicalgetdata ("name");        Console.WriteLine (a); }

Note the notes inside.

How parameters are passed between different threads in the same application domain in C #

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.