public void CaptureContextNoSyncContextTest()
        {
            var originalContext = SynchronizationContext.Current;
            var syncContext     = new CustomSyncContext();

            try
            {
                SynchronizationContext.SetSynchronizationContext(syncContext);
                var eContext = ExecutionContextHelper.CaptureContextNoSyncContextIfPossible();
                Assert.IsNotNull(eContext);

                AtomicBool isDefaulContext = new AtomicBool(false);
                var        task            = Task.Run(() =>
                {
                    SynchronizationContext.SetSynchronizationContext(null);
                    ExecutionContextHelper.RunInContext(eContext, (st) =>
                    {
                        isDefaulContext.Value = SynchronizationContext.Current == null;
                    }, null, true);
                });


                task.Wait();
                TimingAssert.IsTrue(10000, isDefaulContext, "Default context expected");
            }
            finally
            {
                SynchronizationContext.SetSynchronizationContext(originalContext);
            }
        }
Beispiel #2
0
        public ExecutionContextHelperTests()
        {
            var authSettings = new AuthSettings
            {
                Secret = ExpectedSecret
            };

            var mockAuthSettingsOption = new Mock <IOptions <AuthSettings> >();

            mockAuthSettingsOption.SetupGet(option => option.Value).Returns(authSettings);
            _executionContextHelper = new ExecutionContextHelper(mockAuthSettingsOption.Object);
        }
Beispiel #3
0
        public void Run(bool restoreExecContext, bool preserveSyncContext)
        {
            UpdateState(ThreadPoolWorkItemState.Started);

            if (restoreExecContext && _сapturedContext != null)
            {
                ExecutionContextHelper.RunInContext(_сapturedContext, RunContextCallback, this, preserveSyncContext);
            }
            else
            {
                RunInner();
            }

            UpdateState(ThreadPoolWorkItemState.Completed);
        }
Beispiel #4
0
        public void CaptureExecutionContext(bool captureSyncContext = false)
        {
            if (!_allowExecutionContextFlow)
            {
                return;
            }

            if (captureSyncContext)
            {
                _сapturedContext = ExecutionContext.Capture();
            }
            else
            {
                _сapturedContext = ExecutionContextHelper.CaptureContextNoSyncContext();
            }
        }
Beispiel #5
0
        private static void CapturePerfNew(int elemCount)
        {
            ExecutionContext data = ExecutionContextHelper.CaptureContextNoSyncContextIfPossible();
            ContextCallback  clb  = new ContextCallback(ExecutionMethod);

            SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());

            Stopwatch sw = Stopwatch.StartNew();

            for (int i = 0; i < elemCount; i++)
            {
                data = ExecutionContextHelper.CaptureContextNoSyncContextIfPossible();
                ExecutionContextHelper.RunInContext(data, clb, null, true);
                //DoWithContext(data);
            }

            sw.Stop();

            Console.WriteLine("CapturePerfNew. Time = " + sw.ElapsedMilliseconds.ToString() + "ms");
            Console.WriteLine();
        }