了解Silverlight Dispatcher

时间:2010-04-05 22:49:50

标签: c# wcf silverlight asynchronous dispatcher

我遇到了无效的跨线程访问问题,但是我进行了一些研究,并设法使用Dispatcher修复它。

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

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

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

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

所以问题是通过调用

来解决的
Deployment.Current.Dispatcher.BeginInvoke( () => dataContext.Detail = detail );

在我班上。

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

2 个答案:

答案 0 :(得分:21)

理想情况下,存储一个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
{
    /// <summary>
    /// A smart dispatcher system for routing actions to the user interface
    /// thread.
    /// </summary>
    public static class SmartDispatcher
    {
        /// <summary>
        /// A single Dispatcher instance to marshall actions to the user
        /// interface thread.
        /// </summary>
        private static Dispatcher _instance;

        /// <summary>
        /// Backing field for a value indicating whether this is a design-time
        /// environment.
        /// </summary>
        private static bool? _designer;

        /// <summary>
        /// Requires an instance and attempts to find a Dispatcher if one has
        /// not yet been set.
        /// </summary>
        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.");
            }
        }

        /// <summary>
        /// Initializes the SmartDispatcher system, attempting to use the
        /// RootVisual of the plugin to retrieve a Dispatcher instance.
        /// </summary>
        public static void Initialize()
        {
            if (_instance == null)
            {
                RequireInstance();
            }
        }

        /// <summary>
        /// Initializes the SmartDispatcher system with the dispatcher
        /// instance.
        /// </summary>
        /// <param name="dispatcher">The dispatcher instance.</param>
        public static void Initialize(Dispatcher dispatcher)
        {
            if (dispatcher == null)
            {
                throw new ArgumentNullException("dispatcher");
            }

            _instance = dispatcher;

            if (_designer == null)
            {
                _designer = DesignerProperties.IsInDesignTool;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static bool CheckAccess()
        {
            if (_instance == null)
            {
                RequireInstance();
            }

            return _instance.CheckAccess();
        }

        /// <summary>
        /// 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.
        /// </summary>
        /// <param name="a">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.</param>
        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);
            }
        }
    }
}

答案 1 :(得分:6)

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

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

DispatcherHelper.Initialize();

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

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