C # Thread Local Storage Call context logic call context

Source: Internet
Author: User

Thread Local Storage
usingSystem;usingSystem.Threading;usingSystem.Threading.Tasks;namespaceconsoleapptest{classProgram {Static voidMain (string[] args)        {threaddataslottest.test (); }    }    /// <summary>    ///Thread Local Storage/// </summary>    classThreaddataslottest { Public Static voidTest () { for(vari =0; I <Ten; i++) {Thread.Sleep (Ten); Task.run (()=                {                    varSlot = Thread.getnameddataslot ("Test"); if(Slot = =NULL) {Thread.allocatenameddataslot ("Test"); }                    if(Thread.getdata (slot) = =NULL) {Thread.setdata (slot, DateTime.Now.Millisecond); } Console.WriteLine (Thread.CurrentThread.ManagedThreadId+":"+Thread.getdata (slot));            });        } console.readline (); }    }}

If you are using a thread pool, it is best not to use this storage mechanism because the thread pool may not release used threads, resulting in possible sharing of data between multiple executions (data that is stored locally by the thread can be reset before each execution).

Invoke context
usingSystem;usingSystem.Runtime.Remoting.Messaging;usingSystem.Threading;usingSystem.Threading.Tasks;namespaceconsoleapptest{classProgram {Static voidMain (string[] args)        {callcontexttest.test (); }    }    /// <summary>    ///Invoke Context/// </summary>    classCallcontexttest { Public Static voidTest () {if(CallContext.GetData ("Test") ==NULL) {Callcontext.setdata ("Test","Callcontext.setdata"); }             for(vari =0; I <Ten; i++) {Thread.Sleep (Ten); Task.run (()=                {                    if(CallContext.GetData ("Test") ==NULL) {Callcontext.setdata ("Test", DateTime.Now.Millisecond); } Console.WriteLine (Thread.CurrentThread.ManagedThreadId+":"+ CallContext.GetData ("Test"));            });        } console.readline (); }    }}

It is possible to know that each execution of the data is completely isolated and very much in line with our expectations. However, if we expect a child thread to be turned on during the call, how do we let the child thread access the data of the parent thread? This needs to be used to: "Logical call Context".

Logical call Context
usingSystem;usingSystem.Runtime.Remoting.Messaging;usingSystem.Threading;usingSystem.Threading.Tasks;namespaceconsoleapptest{classProgram {Static voidMain (string[] args)        {executioncontexttest.test (); }    }    /// <summary>    ///Invoke Context/// </summary>    classExecutioncontexttest { Public Static voidTest () {Console.WriteLine ("Test: Callcontext.setdata"); Task.run (()={callcontext.setdata ("Test","Wolf"); Console.WriteLine (Thread.CurrentThread.ManagedThreadId+":"+ CallContext.GetData ("Test")); Task.run (()={Console.WriteLine (Thread.CurrentThread.ManagedThreadId+":"+ CallContext.GetData ("Test"));            });            }); Thread.Sleep ( -); Console.WriteLine ("Test: Callcontext.logicalsetdata"); Task.run (()={callcontext.logicalsetdata ("Test","Wolf"); Console.WriteLine (Thread.CurrentThread.ManagedThreadId+":"+ Callcontext.logicalgetdata ("Test")); Task.run (()={Console.WriteLine (Thread.CurrentThread.ManagedThreadId+":"+ Callcontext.logicalgetdata ("Test"));                });                Executioncontext.suppressflow (); Task.run (()={Console.WriteLine ("after Suppressflow:"+ Callcontext.logicalgetdata ("Test"));                });                Executioncontext.restoreflow (); Task.run (()={Console.WriteLine ("after Restoreflow:"+ Callcontext.logicalgetdata ("Test"));            });            });        Console.ReadLine (); }    }}

Note Executioncontext.suppressflow (); and Xecutioncontext.restoreflow (), which can block propagation and reset propagation, respectively, which are allowed to propagate by default.

C # Thread Local Storage Call context logic call context

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.