了解Silverlight的调度 [英] Understanding the Silverlight Dispatcher

查看:119
本文介绍了了解Silverlight的调度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个无效的交叉线程访问的问题,而是一个小小的研究,我设法通过调度来解决它。

I had a Invalid Cross Thread access issue, but a little research and I managed to fix it by using the Dispatcher.

现在在我的应用我有延迟加载的对象。我会想办法让使用WCF一个异步调用和往常一样我用的调度来更新我的对象DataContext的,但它并没有为这种情况下工作。这里我却没有找到一个解决方案。这是我不明白。

Now in my app I have objects with lazy loading. I'd make an Async call using WCF and as usual I use the Dispatcher to update my objects DataContext, however it didn't work for this scenario. I did however find a solution here. Here's what I don't understand.

在我的用户我有code来调用的切换方式我的对象。这个方法的调用是一个调度程序中像这样。

In my UserControl I have code to call an Toggle method on my object. The call to this method is within a Dispatcher like so.

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

正如我之前提到的,这是不够的,满足的Silverlight。我不得不做出的其他调度我的对象中调用。我的目标是不会一的UIElement ,但一个简单的类来处理自己所有的加载/保存。

As I mentioned before this was not enough to satisfy Silverlight. I had to make another Dispatcher call within my object. My object is NOT a UIElement, but a simple class that handles all its own loading/saving.

所以,问题得到了解决,通过调用

So the problem was fixed by calling

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

我的类中。

为什么我有打电话给调度员两次实现这一目标?不应该一个高层次的电话就够了吗?有没有之间的差异在 Deployment.Current.Dispatcher 和调度员在的UIElement?

Why did I have to call the Dispatcher twice to achieve this? Shouldn't a high-level call be enough? Is there a difference between the Deployment.Current.Dispatcher and the Dispatcher in a UIElement?

推荐答案

在理想情况下,存储调度员的一个实例,你可以在其他地方使用,而无需在它的线程检查

Ideally, store a single instance of Dispatcher that you can use elsewhere without having the thread check on it.

,调用任何单.Current实例可能实际上会导致调用的跨线程访问检查。先存储它,你就可以避免这个真正得到共享实例。

Calling any singleton .Current instance may in fact cause a cross-thread access check to be invoked. By storing it first, you can avoid this to actually get the shared instance.

我用使用一个调度程序被取消线程时,只是调用否则SmartDispatcher。它解决了这类问题。

I use a "SmartDispatcher" that uses a dispatcher when called off-thread, and just invokes otherwise. It solves this sort of issue.

发表<一个href=\"http://www.jeff.wilcox.name/2010/04/propertychangedbase-crossthread/\">http://www.jeff.wilcox.name/2010/04/propertychangedbase-crossthread/

code:

// (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);
            }
        }
    }
}

这篇关于了解Silverlight的调度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆