public static CulturePreservingExecutionContext Capture()
        {
            // ExecutionContext.SuppressFlow had been called - we expect
            // ExecutionContext.Capture() to return null, so match that
            // behavior and return null.
            if (ExecutionContext.IsFlowSuppressed())
            {
                return(null);
            }

            var culturePreservingContext = new CulturePreservingExecutionContext();

            if (culturePreservingContext._context != null)
            {
                return(culturePreservingContext);
            }
            else
            {
                // If ExecutionContext.Capture() returns null for any other
                // reason besides IsFlowSuppressed, then match that behavior
                // and return null
                culturePreservingContext.Dispose();
                return(null);
            }
        }
        public static void Run(CulturePreservingExecutionContext executionContext, ContextCallback callback, object state)
        {
            if (executionContext == null)
            {
                throw new ArgumentNullException(nameof(executionContext));
            }

            if (callback == null)
            {
                return;                   // Bail out early if callback is null
            }
            // Compat switch is set, defer directly to EC.Run
            if (BaseAppContextSwitches.DoNotUseCulturePreservingDispatcherOperations)
            {
                ExecutionContext.Run(executionContext._context, callback, state);
                return;
            }

            // Save culture information - we will need this to
            // restore just before the callback is actually invoked from
            // CallbackWrapper.
            executionContext._cultureAndContext = CultureAndContextManager.Initialize(callback, state);

            try
            {
                ExecutionContext.Run(
                    executionContext._context,
                    CulturePreservingExecutionContext.CallbackWrapperDelegate,
                    executionContext._cultureAndContext);
            }
            finally
            {
                // Restore culture information - it might have been
                // modified during the callback execution.
                executionContext._cultureAndContext.WriteCultureInfosToCurrentThread();
            }
        }