SynchronizationContextHelper.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / NetFx40 / System.Activities / System / Activities / SynchronizationContextHelper.cs / 1305376 / SynchronizationContextHelper.cs

                            //------------------------------------------------------------------------------ 
// Copyright (c) Microsoft Corporation.  All rights reserved.
//-----------------------------------------------------------------------------

namespace System.Activities 
{
    using System.Runtime; 
    using System.Threading; 
    using System.Security;
 
    static class SynchronizationContextHelper
    {
        static WFDefaultSynchronizationContext defaultContext;
 
        public static SynchronizationContext GetDefaultSynchronizationContext()
        { 
            if (SynchronizationContextHelper.defaultContext == null) 
            {
                SynchronizationContextHelper.defaultContext = new WFDefaultSynchronizationContext(); 
            }
            return SynchronizationContextHelper.defaultContext;
        }
 
        public static SynchronizationContext CloneSynchronizationContext(SynchronizationContext context)
        { 
            Fx.Assert(context != null, "null context parameter"); 
            WFDefaultSynchronizationContext wfDefaultContext = context as WFDefaultSynchronizationContext;
            if (wfDefaultContext != null) 
            {
                Fx.Assert(SynchronizationContextHelper.defaultContext != null, "We must have set the static member by now!");
                return SynchronizationContextHelper.defaultContext;
            } 
            else
            { 
                return context.CreateCopy(); 
            }
        } 

        class WFDefaultSynchronizationContext : SynchronizationContext
        {
            public WFDefaultSynchronizationContext() 
            {
            } 
 
            public override void Post(SendOrPostCallback d, object state)
            { 
                new SendOrPostCallbackActionItem(d, state).PostWithNoContext();
            }

            public override void Send(SendOrPostCallback d, object state) 
            {
                d(state); 
            } 

 
            class SendOrPostCallbackActionItem : ActionItem
            {
                SendOrPostCallback callback;
                object state; 

                public SendOrPostCallbackActionItem(SendOrPostCallback callback, object state) 
                { 
                    this.callback = callback;
                    this.state = state; 
                }

                public void PostWithNoContext()
                { 
                    base.ScheduleWithoutContext();
                } 
 
                protected override void Invoke()
                { 
                    this.callback(state);
                }
            }
        } 
    }
} 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK