使用哪个调度程序?窗口还是控件?
到目前为止,当我想从非 UI 线程更新 UI 控件时,我使用的语法如下:
Dispatcher.Invoke(DispatcherPriority.Normal,
new Action(()=>Label1.Content="New Content"));
现在我正在阅读更多有关它的内容,我不断发现以下语法:
Label1.Dispatcher.Invoke(//same arguments;
后者更好吗?为什么我会选择一种方法而不是另一种?
Up to now when I wanted to update an UI control from a non UI thread I used syntax like:
Dispatcher.Invoke(DispatcherPriority.Normal,
new Action(()=>Label1.Content="New Content"));
Now I am reading some more about it I keep finding the following syntax:
Label1.Dispatcher.Invoke(//same arguments;
Is the latter better? Why would I chose one method over the other?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我个人会使用该控件本身,因为这样您甚至不需要担心知道包含的窗口。
然而,我不认为这有什么关系——至少在正常的 Win32 中,单个窗口中的所有控件都应该由单个 UI 线程“拥有”。我希望这也能延续到 WPF。 (在某些情况下,当一个窗口在另一个窗口中重新设置父级时,这并不适用,但这种情况非常罕见。)
I would personally use the one for the control itself, as then you don't need to worry about even knowing the containing window.
However, I wouldn't expect it to matter - at least in normal Win32, all the controls within a single window ought to be "owned" by a single UI thread. I would expect that to carry over to WPF too. (There are some cases where this doesn't apply, when one window is reparented in another, but that's pretty rare.)