System.Threading.SynchronizationContextSwitcher.UndoNoThrow C# (CSharp) Method

UndoNoThrow() private method

private UndoNoThrow ( ) : bool
return bool
        internal bool UndoNoThrow()
        {
            if (_ec  == null) 
            {
                return true;
            }  

            try
            {
                Undo();
            }
            catch
            {
                return false;
            }
            return true;
        }
        [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]

Usage Example

        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);
        }
All Usage Examples Of System.Threading.SynchronizationContextSwitcher::UndoNoThrow