コード例 #1
0
        public static MessagePumpStrategy FromCurrentSynchronizationContext()
        {
            var context = SynchronizationContext.Current;

            if (context is SingleThreadedTestSynchronizationContext)
            {
                return(SingleThreadedTestMessagePumpStrategy.Instance);
            }

            return(WindowsFormsMessagePumpStrategy.GetIfApplicable()
                   ?? WpfMessagePumpStrategy.GetIfApplicable()
                   ?? NoMessagePumpStrategy.Instance);
        }
コード例 #2
0
            public static MessagePumpStrategy GetIfApplicable()
            {
                if (!IsApplicable(SynchronizationContext.Current))
                {
                    return(null);
                }

                if (_instance is null)
                {
                    var dispatcherType = SynchronizationContext.Current.GetType().Assembly.GetType("System.Windows.Threading.Dispatcher", throwOnError: true);

                    var dispatcherRun = (Action)dispatcherType
                                        .GetMethod("Run", BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly, null, Type.EmptyTypes, null)
                                        .CreateDelegate(typeof(Action));

                    var dispatcherExitAllFrames = (Action)dispatcherType
                                                  .GetMethod("ExitAllFrames", BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly, null, Type.EmptyTypes, null)
                                                  .CreateDelegate(typeof(Action));

                    _instance = new WpfMessagePumpStrategy(dispatcherRun, dispatcherExitAllFrames);
                }

                return(_instance);
            }