Пример #1
0
        public static void TestProgressWithSynchronizationContext()
        {
            SynchronizationContext         synchronizationContext = new SynchronizationContext();
            StateForSynchronizationContext s = new StateForSynchronizationContext();

            s.WaitEvent = new ManualResetEvent(false);
            s.SynchronizationContext = synchronizationContext;
            synchronizationContext.Post(
                (object state) =>
            {
                StateForSynchronizationContext ss = (StateForSynchronizationContext)state;
                SynchronizationContext.SetSynchronizationContext(ss.SynchronizationContext);
                ss.SynchronizationContext = SynchronizationContext.Current;

                ProgressContext progress = new ProgressContext();
                progress.NotifyLevelStart();
                progress.Progressing += (object sender, ProgressEventArgs e) =>
                {
                    ss.DidProgress = true;
                };
                progress.NotifyLevelFinished();
                ss.WaitEvent.Set();
            }, s);
            bool waitOk = s.WaitEvent.WaitOne(TimeSpan.FromSeconds(10), false);

            Assert.That(waitOk, "The wait should not time-out");
            Assert.That(s.SynchronizationContext, Is.EqualTo(synchronizationContext), "The SynchronizationContext should be current in the code executed.");
            Assert.That(s.DidProgress, "There should always be one Progressing event after NotifyFinished().");
        }
Пример #2
0
        public static void TestProgressWithSynchronizationContext()
        {
            SynchronizationContext synchronizationContext = new SynchronizationContext();
            StateForSynchronizationContext s = new StateForSynchronizationContext();
            s.WaitEvent = new ManualResetEvent(false);
            s.SynchronizationContext = synchronizationContext;
            synchronizationContext.Post(
                (object state) =>
                {
                    StateForSynchronizationContext ss = (StateForSynchronizationContext)state;
                    SynchronizationContext.SetSynchronizationContext(ss.SynchronizationContext);
                    ss.SynchronizationContext = SynchronizationContext.Current;

                    ProgressContext progress = new ProgressContext();
                    progress.NotifyLevelStart();
                    progress.Progressing += (object sender, ProgressEventArgs e) =>
                    {
                        ss.DidProgress = true;
                    };
                    progress.NotifyLevelFinished();
                    ss.WaitEvent.Set();
                }, s);
            bool waitOk = s.WaitEvent.WaitOne(TimeSpan.FromSeconds(10), false);
            Assert.That(waitOk, "The wait should not time-out");
            Assert.That(s.SynchronizationContext, Is.EqualTo(synchronizationContext), "The SynchronizationContext should be current in the code executed.");
            Assert.That(s.DidProgress, "There should always be one Progressing event after NotifyFinished().");
        }