ThreadBehavior.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ WCF / WCF / 3.5.30729.1 / untmp / Orcas / SP / ndp / cdf / src / WCF / ServiceModel / System / ServiceModel / Dispatcher / ThreadBehavior.cs / 1 / ThreadBehavior.cs

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

namespace System.ServiceModel.Dispatcher 
{
    using System; 
    using System.ServiceModel; 
    using System.Threading;
    using System.ServiceModel.Channels; 

    class ThreadBehavior
    {
        SendOrPostCallback threadAffinityCallback; 
        static WaitCallback cleanThreadCallback;
        readonly SynchronizationContext context; 
 
        internal ThreadBehavior(DispatchRuntime dispatch)
        { 
            this.context = dispatch.SynchronizationContext;
        }

        SendOrPostCallback ThreadAffinityCallbackDelegate 
        {
            get 
            { 
                if (this.threadAffinityCallback == null)
                { 
                    this.threadAffinityCallback = new SendOrPostCallback(this.SynchronizationContextCallback);
                }
                return this.threadAffinityCallback;
            } 
        }
 
        static WaitCallback CleanThreadCallbackDelegate 
        {
            get 
            {
                if (ThreadBehavior.cleanThreadCallback == null)
                {
                    ThreadBehavior.cleanThreadCallback = new WaitCallback(ThreadBehavior.CleanThreadCallback); 
                }
                return ThreadBehavior.cleanThreadCallback; 
            } 
        }
 
        internal void BindThread(ref MessageRpc rpc)
        {
            SynchronizationContext context = rpc.InstanceContext.SynchronizationContext ?? this.context;
 
            if (context != null)
            { 
                IResumeMessageRpc resume = rpc.Pause(); 
                context.Post(this.ThreadAffinityCallbackDelegate, resume);
            } 
            else if (rpc.SwitchedThreads)
            {
                IResumeMessageRpc resume = rpc.Pause();
                IOThreadScheduler.ScheduleCallback(ThreadBehavior.CleanThreadCallbackDelegate, resume); 
            }
        } 
 
        internal void BindEndThread(ref MessageRpc rpc)
        { 
            this.BindThread(ref rpc);
        }

        void SynchronizationContextCallback(object state) 
        {
            bool alreadyResumedNoLock; 
            ((IResumeMessageRpc)state).Resume(out alreadyResumedNoLock); 

            if (alreadyResumedNoLock) 
            {
                string text = SR.GetString(SR.SFxMultipleCallbackFromSynchronizationContext, context.GetType().ToString());
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(text));
            } 
        }
 
        static void CleanThreadCallback(object state) 
        {
            bool alreadyResumedNoLock; 
            ((IResumeMessageRpc)state).Resume(out alreadyResumedNoLock);

            if (alreadyResumedNoLock)
            { 
                DiagnosticUtility.DebugAssert("IOThreadScheduler called back twice");
            } 
        } 

        internal static SynchronizationContext GetCurrentSynchronizationContext() 
        {
            if (ServiceHostingEnvironment.ApplicationDomainHosted)
            {
                return null; 
            }
 
            return SynchronizationContext.Current; 
        }
    } 
}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
                        

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