C# 了解Silverlight调度器
我有一个无效的跨线程访问问题,但经过一点研究,我通过使用Dispatcher成功地解决了这个问题 现在在我的应用程序中,我有了延迟加载的对象。我会使用WCF进行异步调用,并像往常一样使用Dispatcher更新我的objects DataContext,但是在这种情况下它不起作用。不过,我确实找到了解决办法。这是我不明白的 在我的UserControl中,我有代码来调用对象上的Toggle方法。对该方法的调用是在这样一个调度器中进行的C# 了解Silverlight调度器,c#,wcf,silverlight,asynchronous,dispatcher,C#,Wcf,Silverlight,Asynchronous,Dispatcher,我有一个无效的跨线程访问问题,但经过一点研究,我通过使用Dispatcher成功地解决了这个问题 现在在我的应用程序中,我有了延迟加载的对象。我会使用WCF进行异步调用,并像往常一样使用Dispatcher更新我的objects DataContext,但是在这种情况下它不起作用。不过,我确实找到了解决办法。这是我不明白的 在我的UserControl中,我有代码来调用对象上的Toggle方法。对该方法的调用是在这样一个调度器中进行的 Dispatcher.BeginInvoke( () =&g
Dispatcher.BeginInvoke( () => _CurrentPin.ToggleInfoPanel() );
正如我之前提到的,这还不足以满足Silverlight的要求。我必须在我的对象内进行另一次调度调用。我的对象不是UIElement,而是一个简单的类,它处理自己的所有加载/保存
因此,通过拨打电话解决了问题
Deployment.Current.Dispatcher.BeginInvoke( () => dataContext.Detail = detail );
在我的班级里
为什么我要给调度员打两次电话才能做到这一点?一个高层电话难道还不够吗?部署.Current.Dispatcher和UIElement中的Dispatcher之间是否有区别?理想情况下,存储一个
Dispatcher
的实例,您可以在其他地方使用,而无需对其进行线程检查
调用任何singleton.Current实例实际上可能会导致调用跨线程访问检查。通过先存储它,您可以避免这种情况,从而实际获取共享实例
我使用一个“SmartDispatcher”,它在线程外调用时使用dispatcher,而在其他情况下调用。它解决了这类问题
职位:
代码:
/(c)微软公司版权所有。
//此源受Microsoft公共许可证(Ms PL)的约束。
//请看http://go.microsoft.com/fwlink/?LinkID=131993 详情请参阅。
//保留所有其他权利。
使用系统组件模型;
命名空间System.Windows.Threading
{
///
///用于将操作路由到用户界面的智能调度程序系统
///线。
///
公共静态类SmartDispatcher
{
///
///单个Dispatcher实例向用户发送动作
///接口线程。
///
私有静态调度程序_实例;
///
///用于指示这是否为设计时的值的支持字段
///环境。
///
私人静态布尔?\u设计师;
///
///需要一个实例并尝试查找调度程序(如果有)
///尚未确定。
///
私有静态无效恢复()
{
如果(_designer==null)
{
_设计器=DesignerProperties.IsInDesignTool;
}
//设计时间更多的是无操作,无法解决问题
//如果在这些情况下尚未设置,则为dispatcher。
如果(_designer==true)
{
返回;
}
//尝试使用插件的RootVisual来检索
//dispatcher实例。只有当前
//线程是UI线程。
尝试
{
_实例=Application.Current.RootVisual.Dispatcher;
}
捕获(例外e)
{
抛出新的无效操作异常(“第一次使用SmartDispatcher必须来自用户接口线程。考虑应用程序调用初始化,有或没有实例。”,E);
}
if(_instance==null)
{
抛出新的InvalidOperationException(“无法找到合适的调度程序实例”);
}
}
///
///初始化SmartDispatcher系统,尝试使用
///用于检索Dispatcher实例的插件的RootVisual。
///
公共静态void Initialize()
{
if(_instance==null)
{
要求复职();
}
}
///
///使用调度程序初始化SmartDispatcher系统
///例如。
///
///调度程序实例。
公共静态无效初始化(调度程序)
{
if(dispatcher==null)
{
抛出新的ArgumentNullException(“dispatcher”);
}
_实例=调度程序;
如果(_designer==null)
{
_设计器=DesignerProperties.IsInDesignTool;
}
}
///
///
///
///
公共静态bool CheckAccess()
{
if(_instance==null)
{
要求复职();
}
返回_instance.CheckAccess();
}
///
///在用户界面上异步执行指定的委托
///如果当前线程是用户界面线程,则
///如果不使用调度程序,操作将立即发生。
///
///对不接受参数和参数的方法的委托
///不返回值,该值被推送到调度程序上
///事件队列或立即运行,具体取决于当前线程。
公共静态无效开始触发(动作a)
{
if(_instance==null)
{
要求复职();
}
//如果当前线程是用户界面线程,请跳过
//并直接调用该操作。
if(_instance.CheckAccess()| | | u designer==true)
{
a();
}
其他的
{
_例如:BeginInvoke(a);
}
}
}
}
如果使用,则可以使用Extras dll中Galasoft.MvvmLight.Threading命名空间中的DispatcherHelper类。它检查访问并为调度器使用静态属性,类似于
// (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);
}
}
}
}
DispatcherHelper.Initialize();
DispatcherHelper.CheckBeginInvokeOnUI(() => // do stuff; );