了解Silverlight Dispatcher

我有一个无效的跨线程访问问题,但有一点研究,我设法通过使用Dispatcher修复它。

现在在我的应用程序中,我有延迟加载的对象。 我使用WCF进行异步调用,像往常一样,我使用Dispatcher更新我的对象DataContext,但它不适用于这种情况。 不过我在这里找到了解决方案。 这是我不明白的。

在我的UserControl中,我有代码在我的对象上调用Toggle方法。 对此方法的调用在Dispatcher中是这样的。

Dispatcher.BeginInvoke( () => _CurrentPin.ToggleInfoPanel() ); 

正如我之前提到的,这还不足以满足Silverlight。 我必须在我的对象中进行另一个 Dispatcher调用。 我的对象不是UIElement ,而是一个处理所有自己的加载/保存的简单类。

所以问题是通过调用来解决的

 Deployment.Current.Dispatcher.BeginInvoke( () => dataContext.Detail = detail ); 

在我class上。

为什么我必须两次调用Dispatcher来实现这一目标? 高级电话不应该足够吗? 在UIElement中Deployment.Current.Dispatcher和Dispatcher之间有区别吗?

理想情况下,存储一个Dispatcher实例,您可以在其他地方使用它而不需要对其进行线程检查。

调用任何单例。当前实例实际上可能导致调用跨线程访问检查。 通过先存储它,您可以避免这种情况实际获取共享实例。

我使用“SmartDispatcher”,在调用off-thread时使用调度程序,只是调用。 它解决了这类问题。

发布: http : //www.jeff.wilcox.name/2010/04/propertychangedbase-crossthread/

码:

 // (c) Copyright Microsoft Corporation. // This source is subject to the Microsoft Public License (Ms-PL). // Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details. // All other rights reserved. 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); } } } } 

如果使用MVVM light工具包 ,则可以使用Extras dll中Galasoft.MvvmLight.Threading命名空间中的DispatcherHelper类。 它检查访问权限并为调度程序使用静态属性,类似于SmartDispatcher。

在App.xaml.cs启动事件调用中:

 DispatcherHelper.Initialize(); 

然后,您需要使用调度程序的任何地方:

  DispatcherHelper.CheckBeginInvokeOnUI(() => // do stuff; );