WPF可以有多个GUI线程吗?或者它总是只有一个GUI线程(即使我有多个窗口/对话框)?可以/不可以WPF有多个GUI线程?
我在问,因为我有来自其他线程的事件,我想在GUI线程中处理它们(因为我需要根据事件修改主窗口的控件)。
btw:我知道我需要使用Dispatcher
对象来达到此目的。所以,我可以改述我的问题,并问:WPF中的所有GUI元素总是只有一个Dispatcher
对象?
WPF可以有多个GUI线程吗?或者它总是只有一个GUI线程(即使我有多个窗口/对话框)?可以/不可以WPF有多个GUI线程?
我在问,因为我有来自其他线程的事件,我想在GUI线程中处理它们(因为我需要根据事件修改主窗口的控件)。
btw:我知道我需要使用Dispatcher
对象来达到此目的。所以,我可以改述我的问题,并问:WPF中的所有GUI元素总是只有一个Dispatcher
对象?
基于第一个答案中的链接,我自己做了一些验证。我想在此分享结果。首先:
可以有多个GUI线程(因此可以有多个Dispatcher
实例)。
但是:
只需在创建一个新的窗口(模与否)不创建一个新的GUI线程。需要明确创建线程(通过创建Thread
的新实例)。
注:而不是使用单独的线程中,模态对话框很可能是通过利用Dispatcher.PushFrame()哪些块被分派此方法的调用者,同时仍允许事件实现。
我已经创建了一个简单的WPF类(再次,基于第一个答案的链接)来验证所有这些东西。我在这里分享它,所以你可以稍微玩一下。
MainWindow.xaml:
<Window x:Class="WindowThreadingTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="250" Height="130">
<StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Thread's ID is "/>
<TextBlock x:Name="m_threadId"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Thread's threading apartment is "/>
<TextBlock x:Name="m_threadTA"/>
</StackPanel>
<Button Click="OnCreateNewWindow" Content="Open New Window"/>
<Button Click="OnAccessTest" Content="Access Test"/>
</StackPanel>
</Window>
MainWindow.xaml.cs:
using System;
using System.Threading;
using System.Windows;
using System.Windows.Media;
namespace WindowThreadingTest {
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window {
private static uint s_windowNumber = 0;
private readonly MainWindow m_prevWindow;
public MainWindow() : this(null) { }
public MainWindow(MainWindow prevWindow) {
InitializeComponent();
this.m_prevWindow = prevWindow;
this.Title = String.Format("Window {0}", ++s_windowNumber);
Thread thread = Thread.CurrentThread;
this.m_threadId.Text = thread.ManagedThreadId.ToString();
this.m_threadTA.Text = thread.GetApartmentState().ToString();
}
private void OnCreateNewWindow(object sender, RoutedEventArgs e) {
CreateNewWindow(true, false, true);
}
private void CreateNewWindow(bool newThread, bool modal, bool showInTaskbar) {
MainWindow mw = this;
if (newThread) {
Thread thread = new Thread(() => {
MainWindow w = new MainWindow(this);
w.ShowInTaskbar = showInTaskbar;
if (modal) {
// ShowDialog automatically starts the event queue for the new windows in the new thread. The window isn't
// modal though.
w.ShowDialog();
} else {
w.Show();
w.Closed += (sender2, e2) => w.Dispatcher.InvokeShutdown();
System.Windows.Threading.Dispatcher.Run();
}
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
} else {
MainWindow w = new MainWindow(this);
w.ShowInTaskbar = showInTaskbar;
if (modal) {
// Even modal dialogs run in the same thread.
w.ShowDialog();
} else {
w.Show();
}
}
}
private void OnAccessTest(object sender, RoutedEventArgs e) {
if (m_prevWindow == null) {
return;
}
this.Background = Brushes.Lavender;
try {
m_prevWindow.Background = Brushes.LightBlue;
} catch (InvalidOperationException excpt) {
MessageBox.Show("Exception: " + excpt.Message, "Invalid Operation");
}
m_prevWindow.Dispatcher.Invoke((Action)(() => m_prevWindow.Background = Brushes.Green));
this.Dispatcher.Invoke((Action)(() => {
try {
m_prevWindow.Background = Brushes.Red;
} catch (InvalidOperationException excpt) {
MessageBox.Show("Exception: " + excpt.Message, "Invalid Dispatcher Operation");
}
}));
}
}
}
那么如果你不能,那么你不能,+1 – CloudyMarble 2013-02-21 13:36:25
是否已经阅读了WPF线程模型文档? http://msdn.microsoft.com/en-us/library/ms741870.aspx – 2011-04-19 13:13:18