public void AwaitableEntrance() { var n = 0; var r = new AnonymousSynchronizationContext(a => { n += 1; a(); }); Task.Factory.StartNew(async () => { SynchronizationContext.Current.AssertDoesNotEqual(r); n.AssertEquals(0); await r.AwaitableEntrance(forceReentry: true); SynchronizationContext.Current.AssertEquals(r); n.AssertEquals(1); await r.AwaitableEntrance(forceReentry: true); SynchronizationContext.Current.AssertEquals(r); n.AssertEquals(2); await r.AwaitableEntrance(forceReentry: false); SynchronizationContext.Current.AssertEquals(r); n.AssertEquals(2); await new SynchronizationContext(); await r.AwaitableEntrance(forceReentry: false); SynchronizationContext.Current.AssertEquals(r); n.AssertEquals(3); }).Unwrap().AssertRanToCompletion(); }
public void AnonymousPost() { var n = 0; var m = 0; SynchronizationContext r = null; r = new AnonymousSynchronizationContext(a => { n += 1; var c = SynchronizationContext.Current; a(); SynchronizationContext.Current.AssertDoesNotEqual(r); SynchronizationContext.Current.AssertEquals(c); }); n.AssertEquals(0); m.AssertEquals(0); r.Post(x => { SynchronizationContext.Current.AssertEquals(r); m += 1; }, null); n.AssertEquals(1); m.AssertEquals(1); }