WPF 多线程 UI - 用户控件
我在 WPF 中看到了一些使用多个窗口的多线程 UI 的代码示例,其中每个窗口都在其自己的 UI 线程上运行。不过我很好奇 - 有没有办法通过嵌入式控件来完成此任务?
例如,假设我将 usercontrol1 和 usercontrol2 都嵌入到 Window1 中。 Usercontrol1 开始旋转并阻塞主 UI 线程。窗口和 usercontrol2 受到影响。有没有办法让即使 usercontrol1 阻塞,Window1 + usercontrol2 仍然响应?
让我们假设我们不控制 usercontrol1 的开发人员 - 所以我们不能告诉他们让他们的控件发挥作用。
我们还假设 usercontrol1 + 2 之间的数据交换是必须的。
我应该探索像 AddIn 这样的东西吗?
I have seen some code samples of multi-threaded UIs in WPF using multiple windows where each window runs on it's own UI thread. I'm curious though - is there a way to accomplish this with embedded controls?
For example lets say I have usercontrol1 and usercontrol2 both embedded in Window1. Usercontrol1 starts to spin and blocks the main UI thread. The window and usercontrol2 are effected. Is there a way to make it so even if usercontrol1 blocks, Window1 + usercontrol2 are still responsive?
Let us assume we don't control the developers of usercontrol1 - so we can't tell them to make their control behave.
Let us also assume the exchange of data between usercontrol1 + 2 is a must.
Should I be exploring something like AddIn's?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如顶部所示,您有视觉效果(窗口)继承自 DispatcherObject。它被设计为STA线程模型,其中单线程访问对象的方法和属性。
这就是为什么两个控件必须在与 Window 相同的线程中创建的原因。
如果您需要在控件之间交换数据并且您不控制这些控件的开发人员,我会考虑使用 PRISM 或其他 MVVM 框架。在棱镜中,您有 EventAggregator 这是基本上,订阅者发布者模式您的 UserControl1 可以发布某些内容,而另一个(如果已订阅)将收到该内容。
As on the top you have visual (Window) which is inheriting from DispatcherObject. It is designed as STA threading model, in which single thread is accessing the object methods and properties.
That is why both controls must be created in the same thread as Window.
If you need to exchange data between controls and you are not controlling developers of those controls I would consider using PRISM or other MVVM framework. In prism you have EventAggregator which is basically subscriber publisher pattern your UserControl1 can publish something and the other one if it's subscribed will receive that.
您无法在另一个线程上“运行”控件(与 Winform 一样,控件本身必须在与顶级父级相同的线程上创建和运行),但没有什么可以阻止您启动 来自用户控件的另一个线程上的操作。您只需确保使用
Dispatcher
来调用
任何对控件本身有直接影响的操作。不过,幕后处理可以完全在另一个线程上完成;这只是 UI 的物理更新,必须通过 Dispatcher 调用回来。编辑:编辑问题后,不,没有办法将特定组件执行的所有“工作”移至另一个线程。如果您无法控制相关组件的开发,那么您就会受到开发人员以及他决定执行代码的位置的支配。
You can't "run" the controls on another thread (as with Winforms, the controls themselves must be created and run on the same thread as the top-level parent), but there's nothing stopping you from initiating actions on another thread from a user control. You just have to ensure that you use the
Dispatcher
toInvoke
any operations that will have a direct effect upon the control itself. Behind-the-scenes processing can be done entirely on another thread, though; it's just the physical updating of the UI that has to be invoked back via theDispatcher
.EDIT: After the question was edited, no, there is no way to move all of the "work" that a particular component performs to another thread. If you can't control the development of the component in question, then you're at the mercy of the developer and where he decides to execute the code.