DispatcherHelper
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了DispatcherHelper相关的知识,希望对你有一定的参考价值。
/// <summary> /// 使用之前在App.Config中初始化Initialize() /// </summary> public static class DispatcherHelper { public static Dispatcher UiDispatcher { get; private set; } /// <summary> /// 检查操作是否是在UI线程上。假如操作在UI线程上则直接执行,否则调用UI Dispatcher线程执行。 /// </summary> /// <param name="action"></param> public static void CheckBeginInvokeOnUi(Action action) { if (action == null) return; CheckDispatcher(); if (UiDispatcher.CheckAccess()) { action(); } else { UiDispatcher.BeginInvoke(action); } } private static void CheckDispatcher() { if (UiDispatcher == null) { var error = new StringBuilder("The DispatcherHelper is not initialized."); error.AppendLine(); throw new InvalidOperationException(error.ToString()); } } /// <summary> /// 在UI线程上执行异步操作 /// </summary> /// <param name="action"></param> /// <returns></returns> public static DispatcherOperation RunAsync(Action action) { CheckDispatcher(); return UiDispatcher.BeginInvoke(action); } /// <summary> /// 在APP.Config的Application_Startup中初始化Initialize /// </summary> public static void Initialize() { if (UiDispatcher != null && UiDispatcher.Thread.IsAlive) { return; } UiDispatcher = Dispatcher.CurrentDispatcher; } }
以上是关于DispatcherHelper的主要内容,如果未能解决你的问题,请参考以下文章