WPF 多线程 UI - 用户控件

发布于 2024-10-14 06:57:48 字数 389 浏览 7 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

人海汹涌 2024-10-21 06:57:49

如顶部所示,您有视觉效果(窗口)继承自 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.

天煞孤星 2024-10-21 06:57:48

您无法在另一个线程上“运行”控件(与 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 to Invoke 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 the Dispatcher.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文