コード例 #1
0
        internal static SynchronizationContextSwitcher SetSynchronizationContext(SynchronizationContext syncContext, SynchronizationContext prevSyncContext)
        {
            // get current execution context
            ExecutionContext ec = Thread.CurrentThread.ExecutionContext;
            // create a swticher
            SynchronizationContextSwitcher scsw = new SynchronizationContextSwitcher();

            RuntimeHelpers.PrepareConstrainedRegions();
            try
            {
                // attach the switcher to the exec context
                scsw._ec = ec;
                // save the current sync context using the passed in value
                scsw.savedSC = prevSyncContext;
                // save the new sync context also
                scsw.currSC = syncContext;
                // update the current sync context to the new context
                ec.SynchronizationContext = syncContext;
            }
            catch
            {
                // Any exception means we just restore the old SyncCtx
                scsw.UndoNoThrow(); //No exception will be thrown in this Undo()
                throw;
            }
            // return switcher
            return(scsw);
        }
        public override bool Equals(object obj)
        {
            if ((obj == null) || !(obj is SynchronizationContextSwitcher))
            {
                return(false);
            }
            SynchronizationContextSwitcher switcher = (SynchronizationContextSwitcher)obj;

            return(((this.savedSC == switcher.savedSC) && (this.currSC == switcher.currSC)) && (this._ec == switcher._ec));
        }
コード例 #3
0
        public override bool Equals(Object obj)
        {
            if (obj == null || !(obj is SynchronizationContextSwitcher))
            {
                return(false);
            }
            SynchronizationContextSwitcher sw = (SynchronizationContextSwitcher)obj;

            return(this.savedSC == sw.savedSC && this.currSC == sw.currSC && this._ec == sw._ec);
        }
 internal static SynchronizationContextSwitcher SetSynchronizationContext(SynchronizationContext syncContext, SynchronizationContext prevSyncContext)
 {
     ExecutionContext executionContext = Thread.CurrentThread.ExecutionContext;
     SynchronizationContextSwitcher switcher = new SynchronizationContextSwitcher();
     RuntimeHelpers.PrepareConstrainedRegions();
     try
     {
         switcher._ec = executionContext;
         switcher.savedSC = prevSyncContext;
         switcher.currSC = syncContext;
         executionContext.SynchronizationContext = syncContext;
     }
     catch
     {
         switcher.UndoNoThrow();
         throw;
     }
     return switcher;
 }
コード例 #5
0
        internal static SynchronizationContextSwitcher SetSynchronizationContext(SynchronizationContext syncContext, SynchronizationContext prevSyncContext)
        {
            ExecutionContext executionContext       = Thread.CurrentThread.ExecutionContext;
            SynchronizationContextSwitcher switcher = new SynchronizationContextSwitcher();

            RuntimeHelpers.PrepareConstrainedRegions();
            try
            {
                switcher._ec     = executionContext;
                switcher.savedSC = prevSyncContext;
                switcher.currSC  = syncContext;
                executionContext.SynchronizationContext = syncContext;
            }
            catch
            {
                switcher.UndoNoThrow();
                throw;
            }
            return(switcher);
        }
コード例 #6
0
        internal static SynchronizationContextSwitcher SetSynchronizationContext(SynchronizationContext syncContext, SynchronizationContext prevSyncContext)
        {
            // get current execution context
            ExecutionContext ec = Thread.CurrentThread.ExecutionContext;
            // create a swticher
            SynchronizationContextSwitcher scsw = new SynchronizationContextSwitcher();

            RuntimeHelpers.PrepareConstrainedRegions();
            try
            {
                // attach the switcher to the exec context
                scsw._ec = ec;
                // save the current sync context using the passed in value 
                scsw.savedSC = prevSyncContext;
                // save the new sync context also
                scsw.currSC = syncContext;
                // update the current sync context to the new context
                ec.SynchronizationContext = syncContext;
            }
            catch
            {
                // Any exception means we just restore the old SyncCtx
                scsw.UndoNoThrow(); //No exception will be thrown in this Undo()
                throw;
            }
            // return switcher
            return scsw;
        }