C# 从 Windows 窗体应用程序在另一个线程中打开 WPF 窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/729584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 22:52:28  来源:igfitidea点击:

Opening a WPF Window in another Thread from a Windows Forms App

c#wpfwinformsmultithreading

提问by MrEdmundo

OK, I should start this off by saying I'm not sure this is necessarily the right way to deal with this but...

好的,我应该首先说我不确定这是否一定是处理这个问题的正确方法,但是......

Basically I have created a Window in WPF for displaying notifications in my application (a bit like Outlook New Mail notification). I would like to try and show this Window in it's own thread (it might do some work in the future).

基本上,我在 WPF 中创建了一个窗口,用于在我的应用程序中显示通知(有点像 Outlook 新邮件通知)。我想尝试在它自己的线程中显示这个窗口(它可能在未来做一些工作)。

I've created the Window using WPF because it's a bit nicer at handling things like AlwaysOnTop and Fading In and Out.

我使用 WPF 创建了窗口,因为它在处理 AlwaysOnTop 和淡入淡出等方面更好一些。

The application that shows the notification is a 3.5 Windows Forms App. I've seen examples similar to this SOF: C# winforms startup (Splash) form not hidingfor showing a Form in a different thread however I'm unable to start a new Message Loop with the WPF Window.

显示通知的应用程序是 3.5 Windows 窗体应用程序。我见过与此SOF类似的示例:C# winforms 启动 (Splash) 表单未隐藏以在不同线程中显示表单,但是我无法使用 WPF 窗口启动新的消息循环。

I've tried just calling Notification.Show() in a new thread however the Window never displays.

我试过在新线程中调用 Notification.Show() 但是窗口永远不会显示。

  1. Is it possible to show this WPF in it's own thread?

  2. Why do I see some resources say that you should not show any UI in a separate thread?

  1. 是否可以在它自己的线程中显示这个 WPF?

  2. 为什么我看到一些资源说你不应该在单独的线程中显示任何 UI?

采纳答案by Jon Skeet

The WPF Threading Modelhas details on this. Look for "Multiple Windows, Multiple Threads."

WPF线程模型对这个细节。查找“多个窗口,多个线程”。

Basically, you start a new thread, which creates the relevant window and then calls

基本上,您启动一​​个新线程,该线程创建相关窗口,然后调用

System.Windows.Threading.Dispatcher.Run();

in order to run a dispatcher for that window, on the new thread.

为了在新线程上为该窗口运行调度程序。

回答by Gerco Dries

You should display the notification using the main UI thread and if that window is going to perform any work, run that work in a background thread. You should not use more than one thread to access or create UI objects, not even if you don't share those objects between threads.

您应该使用主 UI 线程显示通知,如果该窗口要执行任何工作,请在后台线程中运行该工作。您不应使用多个线程来访问或创建 UI 对象,即使您不在线程之间共享这些对象也是如此。

回答by Kent Boogaart

  1. Is it possible to show this WPF in it's own thread?
  1. 是否可以在它自己的线程中显示这个 WPF?

Absolutely. Just create a new STA thread and create and display your Windowfrom it. I've used this technique for a splash screen because the main (UI) thread got really tied up doing other things. It was completely out of my control how much work was being done on the UI thread because of a 3rd party framework we were using.

绝对地。只需创建一个新的 STA 线程并Window从中创建和显示您的线程即可。我已经将这种技术用于启动画面,因为主(UI)线程确实被束缚在做其他事情上。由于我们使用的是第 3 方框架,因此在 UI 线程上完成了多少工作完全不受我的控制。

  1. Why do I see some resources say that you should not show any UI in a separate thread?
  1. 为什么我看到一些资源说你不应该在单独的线程中显示任何 UI?

Possibly because of the extra complexity involved. If one of your Windows wants to pass data to another, you need to remember that they're executing on separate threads. Therefore, each should use its Dispatcher to marshal calls to the correct thread.

可能是因为所涉及的额外复杂性。如果您Window的一个s 想要将数据传递给另一个,您需要记住它们是在不同的线程上执行的。因此,每个人都应该使用其 Dispatcher 将调用编组到正确的线程。