Dispatcher.CheckAccess的正确使用(或不使用)() [英] Correct usage (or not-usage) of Dispatcher.CheckAccess()

查看:676
本文介绍了Dispatcher.CheckAccess的正确使用(或不使用)()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在的WinForms,所有控件有一个 InvokeRequired 属性,返回true,如果我有打电话。[开始],以修改它的控制调用。

In Winforms, all controls have an InvokeRequired property, that returns true if I have to call .[Begin]Invoke on the control in order to modify it.

在WPF中,没有在 DispatcherObject.CheckAccess()和调度。的checkAccess(),但我的 EditorBrowsable(EditorBrowsableState.Never)受惊属性。当我禁用编辑器浏览这个样子,我用它的意思是你应该的的做这个,没有,真的。如果这是需要解决的迫在眉睫的问题,你有错设计的解决方案,以你的首要问题。在另一方面,唯一的办法我发现(而且,事实上,我原来的解决方案)为 Thread.CurrentThread.ManagedThreadId == 1 。 (这太可怕了。而且它不会在一般的情况下工作。我知道,它的工作对我的用途有限,但。)

In WPF, there is an apparently similar construct in DispatcherObject.CheckAccess() and Dispatcher.CheckAccess(), but I am frightened by the EditorBrowsable(EditorBrowsableState.Never) attribute. When I disable editor browsing like this, I use it to means "You should not be doing this. No, really. If this is required solve your immediate problem, you have mis-designed your solution to your overarching problem." On the other hand, the only alternative I've found (and, in fact, my original solution) is Thread.CurrentThread.ManagedThreadId == 1. (It's horrible. And it doesn't work in the generic case. I know. It does work for my limited uses, though.)

MSDN documentation 是上的存在和推理背后的无声 EditorBrowsable 属性。是否确实意味着不要使用这个,因为它会如果我输入了,还是有其他一些不太昂贵的含义?

The MSDN documentation is silent on the presence of and reasoning behind the EditorBrowsable attribute. Does it indeed mean "do not use this", as it would if I had typed it, or does it have some other less prohibitive meaning?

推荐答案

在WPF中,你可以叫 Dispatcher.Invoke 不管你当前线程,并且它会相应处理呼叫 - 如果你已经在正确的线程,它会只调用你的代码,它使用的checkAccess 来处理这种行为。

In WPF, you can call Dispatcher.Invoke regardless of your current thread, and it'll handle the call accordingly - if you're already on the right thread, it'll just invoke your code, and it uses CheckAccess to handle this behaviour.

对于的BeginInvoke 你当前线程是无关紧要的:的BeginInvoke 总是asyncronous,以及执行的顺序取决于你添加到调度的队列项目的优先级

For a BeginInvoke the thread you're currently on is irrelevant: BeginInvoke is always asyncronous, and the order of execution is dependant on the priority of the item you add to the dispatcher's queue.

如果你不应该在所有使用的方法,它不会是市民:那意图属性仅仅是为了掩盖从机制,如智能感知和其他编辑器的浏览器中的成员。您通常不需要使用 Dispatcher.CheckAccess()自己,这可能是为什么它被标记为不可浏览,但这种智慧是我们只能猜测在(除非埃里克利珀是看; - )

If you weren't supposed to use the method at all, it wouldn't be public: the intent of that attribute is only to hide the member from mechanisms such as Intellisense and other editor-browsers. You don't typically need to use Dispatcher.CheckAccess() yourself, which is probably why it's marked as non-browsable, but the wisdom of this is something we can only guess at (unless Eric Lippert is watching ;-)

在总结:只需拨打 Dispatcher.Invoke 和不担心的checkAccess

In summary: just call Dispatcher.Invoke and don't worry about CheckAccess.

这篇关于Dispatcher.CheckAccess的正确使用(或不使用)()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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