SetSynchronizationContext(SynchronizationContext syncContext, SynchronizationContext prevSyncContext) { // get current execution context ExecutionContext ec = Thread.CurrentThread.ExecutionContext; // create a switcher 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 sw = (SynchronizationContextSwitcher)obj; return(this.savedSC == sw.savedSC && this.currSC == sw.currSC && this._ec == sw._ec); }