首页 > SmartDispatcher 类

SmartDispatcher 类

 

 

UI线程中使用

 

public class SmartDispatcher
{ 
    public static void BeginInvoke(Action action)
    { 
        if (Deployment.Current.Dispatcher.CheckAccess() 
            || DesignerProperties.IsInDesignTool)
        { 
            action();
        }
        else
        { 
            Deployment.Current.Dispatcher.BeginInvoke(action);
        }
    }
}
 
 
using System.ComponentModel;
 
namespace System.Windows.Threading
{ 
    /// 
    /// A smart dispatcher system for routing actions to the user interface
    /// thread.
    /// 
    public static class SmartDispatcher
    { 
        /// 
        /// A single Dispatcher instance to marshall actions to the user
        /// interface thread.
        /// 
        private static Dispatcher _instance;
 
        /// 
        /// Backing field for a value indicating whether this is a design-time
        /// environment.
        /// 
        private static bool? _designer;
         
        /// 
        /// Requires an instance and attempts to find a Dispatcher if one has
        /// not yet been set.
        /// 
        private static void RequireInstance()
        { 
            if (_designer == null)
            { 
                _designer = DesignerProperties.IsInDesignTool;
            }
 
            // Design-time is more of a no-op, won't be able to resolve the
            // dispatcher if it isn't already set in these situations.
            if (_designer == true)
            { 
                return;
            }
 
            // Attempt to use the RootVisual of the plugin to retrieve a
            // dispatcher instance. This call will only succeed if the current
            // thread is the UI thread.
            try
            { 
                _instance = Application.Current.RootVisual.Dispatcher;
            }
            catch (Exception e)
            { 
                throw new InvalidOperationException("The first time SmartDispatcher is used must be from a user interface thread. Consider having the application call Initialize, with or without an instance.", e);
            }
 
            if (_instance == null)
            { 
                throw new InvalidOperationException("Unable to find a suitable Dispatcher instance.");
            }
        }
 
        /// 
        /// Initializes the SmartDispatcher system, attempting to use the
        /// RootVisual of the plugin to retrieve a Dispatcher instance.
        /// 
        public static void Initialize()
        { 
            if (_instance == null)
            { 
                RequireInstance();
            }
        }
 
        /// 
        /// Initializes the SmartDispatcher system with the dispatcher
        /// instance.
        /// 
        /// The dispatcher instance.
        public static void Initialize(Dispatcher dispatcher)
        { 
            if (dispatcher == null)
            { 
                throw new ArgumentNullException("dispatcher");
            }
 
            _instance = dispatcher;
 
            if (_designer == null)
            { 
                _designer = DesignerProperties.IsInDesignTool;
            }
        }
 
        /// 
        /// 
        /// 
        /// 
        public static bool CheckAccess()
        { 
            if (_instance == null)
            { 
                RequireInstance();
            }
 
            return _instance.CheckAccess();
        }
 
        /// 
        /// Executes the specified delegate asynchronously on the user interface
        /// thread. If the current thread is the user interface thread, the
        /// dispatcher if not used and the operation happens immediately.
        /// 
        /// A delegate to a method that takes no arguments and 
        /// does not return a value, which is either pushed onto the Dispatcher 
        /// event queue or immediately run, depending on the current thread.
        public static void BeginInvoke(Action a)
        { 
            if (_instance == null)
            { 
                RequireInstance();
            }
 
            // If the current thread is the user interface thread, skip the
            // dispatcher and directly invoke the Action.
            if (_instance.CheckAccess() || _designer == true)
            { 
                a();
            }
            else
            { 
                _instance.BeginInvoke(a);
            }
        }
    }
}

转载于:https://www.cnblogs.com/shiyix/p/3459066.html

更多相关:

  • 上篇笔记中梳理了一把 resolver 和 balancer,这里顺着前面的流程走一遍入口的 ClientConn 对象。ClientConn// ClientConn represents a virtual connection to a conceptual endpoint, to // perform RPCs. // //...

  • 我的实验是基于PSPNet模型实现二维图像的语义分割,下面的代码直接从得到的h5文件开始往下做。。。 也不知道是自己的检索能力出现了问题还是咋回事,搜遍全网都没有可以直接拿来用的语义分割代码,东拼西凑,算是搞成功了。 实验平台:Windows、VS2015、Tensorflow1.8 api、Python3.6 具体的流程为:...

  • Path Tracing 懒得翻译了,相信搞图形学的人都能看得懂,2333 Path Tracing is a rendering algorithm similar to ray tracing in which rays are cast from a virtual camera and traced through a s...

  • configure_file( [COPYONLY] [ESCAPE_QUOTES] [@ONLY][NEWLINE_STYLE [UNIX|DOS|WIN32|LF|CRLF] ]) 我遇到的是 configure_file(config/config.in ${CMAKE_SOURCE_DIR}/...

  •     直接复制以下代码创建一个名为settings.xml的文件,放到C:UsersAdministrator.m2下即可