Понимание диспетчера Silverlight

У меня была Недопустимая Перекрестная проблема доступа Потока, но небольшому исследованию и мне удалось зафиксировать ее при помощи Диспетчера.

Теперь в моем приложении у меня есть объекты с ленивой загрузкой. Я выполнил бы Асинхронный вызов с помощью WCF, и как обычно я использую Диспетчера для обновления моих объектов DataContext, однако он не работал на этот сценарий. Я действительно однако находил решение здесь. Вот то, что я не понимаю.

В моем UserControl у меня есть код для вызова метода Переключателя на моем объекте. Вызов к этому методу в Диспетчере как так.

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

Как я упомянул, прежде чем этого было недостаточно для удовлетворения Silverlight. Я должен был выполнить другой вызов Диспетчера в своем объекте. Мой объект НЕ является UIElement, а простым классом, который обрабатывает всю его собственную загрузку/сохранение.

Таким образом, проблема была решена путем вызова

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

в моем классе.

Почему я должен был вызвать Диспетчера дважды для достижения этого? Разве высокий уровень не должен звонить быть достаточно? Есть ли различие между Развертыванием. Текущий. Диспетчер и Диспетчер в UIElement?

25
задан Community 23 May 2017 в 11:53
поделиться

1 ответ

В идеале, сохранить единственный экземпляр Dispatcher , который вы можете использовать где-либо еще без проверки потока.

Вызов любого одноэлементного экземпляра .Current может фактически вызвать проверку доступа между потоками. Сохраняя его вначале, вы можете избежать этого, чтобы фактически получить общий экземпляр.

Я использую "SmartDispatcher", который использует диспетчер при вызове вне потока и просто вызывает в противном случае. Это решает такого рода проблемы.

Сообщение: 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);
            }
        }
    }
}
21
ответ дан 28 November 2019 в 21:45
поделиться
Другие вопросы по тегам:

Похожие вопросы: