简体   繁体   English

是在Silverlight中调用Dispatcher.CheckAccess()好的形式吗?

[英]Is calling Dispatcher.CheckAccess() good form in Silverlight?

I wonder if the following code buys any performance gains: 我想知道以下代码是否可以提高性能:

if (Deployment.Current.Dispatcher.CheckAccess())
{
    DoUIWork();
}
else
{
    Deployment.Current.Dispatcher.BeginInvoke(() =>
    DoUIWork());
}

Is the Dispatcher smart enough to short circuit a dispatch to the UI thread if its unnecessary? Dispatcher是否足够聪明,可以在不必要的情况下使对UI线程的调度短路?

I couldn't say whether the dispatcher does anything expensive when dispatching from the UI thread to itself, compared with the check. 与检查相比,我不能说从UI线程向自身进行分派时分派器是否做任何昂贵的事情。 But BeginInvoke from the UI thread may behave differently from executing the operation directly, as it's at least put on the queue rather than invoked immediately. 但是来自UI线程的BeginInvoke的行为可能与直接执行操作有所不同,因为它至少放在队列中而不是立即调用。 You could tell the difference between this and removing the conditional statement if you had code directly afterwards. 如果您之后直接编写了代码,则可以说出这与删除条件语句之间的区别。

Certainly worth being aware of the control flow, enough to know if the difference doesn't matter. 当然值得知道控制流,足以知道区别是否无关紧要。

If it is anything like standard Windows SynchronizationContext (and it probably is) then the two options are not the same. 如果它类似于标准Windows SynchronizationContext(可能是这样),则这两个选项不相同。 BeginInvoke will basicaly queue up the method to be executed by the dispatcher message pump after the current execution of any existing message has been processed. 在处理了任何现有消息的当前执行之后,BeginInvoke基本上会将要由调度程序消息泵执行的方法排队。 In your example the two options be the same if you were to use Invoke instead of BeginInvoke. 在您的示例中,如果要使用Invoke而不是BeginInvoke,则两个选项相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM