DispatcherSynchronizationContext.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 / wpf / src / Base / System / Windows / Threading / DispatcherSynchronizationContext.cs / 1407647 / DispatcherSynchronizationContext.cs

                            using System; 
using System.Threading;
using System.Diagnostics;
using System.ComponentModel;
using System.Security;                       // CAS 
using System.Security.Permissions;           // Registry permissions
 
namespace System.Windows.Threading 
{
    ///  
    ///     SynchronizationContext subclass used by the Dispatcher.
    /// 
    public sealed class DispatcherSynchronizationContext : SynchronizationContext
    { 
        /// 
        ///     Constructs a new instance of the DispatcherSynchroniazationContext 
        ///     using the current Dispatcher. 
        /// 
        public DispatcherSynchronizationContext() : this(Dispatcher.CurrentDispatcher) 
        {
        }

        ///  
        ///     Constructs a new instance of the DispatcherSynchroniazationContext
        ///     using the specified Dispatcher. 
        ///  
        public DispatcherSynchronizationContext(Dispatcher dispatcher)
        { 
            if(dispatcher == null)
            {
                throw new ArgumentNullException("dispatcher");
            } 

            _dispatcher = dispatcher; 
 
            // Tell the CLR to call us when blocking.
            SetWaitNotificationRequired(); 
        }

        /// 
        ///     Synchronously invoke the callback in the SynchronizationContext. 
        /// 
        public override void Send(SendOrPostCallback d, Object state) 
        { 
            _dispatcher.Invoke(DispatcherPriority.Normal, d, state);
        } 

        /// 
        ///     Asynchronously invoke the callback in the SynchronizationContext.
        ///  
        public override void Post(SendOrPostCallback d, Object state)
        { 
            _dispatcher.BeginInvoke(DispatcherPriority.Normal, d, state); 
        }
 
        /// 
        ///     Wait for a set of handles.
        /// 
        ///  
        ///     Critical - Calls WaitForMultipleObjectsEx which has a SUC.
        ///  
        [SecurityCritical] 
        [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.ControlPolicy|SecurityPermissionFlag.ControlEvidence)]
        public override int Wait(IntPtr[] waitHandles, bool waitAll, int millisecondsTimeout) 
        {
            if(_dispatcher._disableProcessingCount > 0)
            {
                // Call into native code directly in order to avoid the default 
                // CLR locking behavior which pumps messages under contention.
                // Even though they try to pump only the COM messages, any 
                // messages that have been SENT to the window are also 
                // dispatched.  This can lead to unpredictable reentrancy.
                return MS.Win32.UnsafeNativeMethods.WaitForMultipleObjectsEx(waitHandles.Length, waitHandles, waitAll, millisecondsTimeout, false); 
            }
            else
            {
                return SynchronizationContext.WaitHelper(waitHandles, waitAll, millisecondsTimeout); 
            }
        } 
 
        /// 
        ///     Create a copy of this SynchronizationContext. 
        /// 
        public override SynchronizationContext CreateCopy()
        {
            // Because we do not contain any state that we want to preserve 
            // in seperate instances, we just return the same synchronization
            // context.  The CLR team assures us this is OK. 
            return this; 
        }
 
        internal Dispatcher _dispatcher;
    }
}
 

// 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