问题描述
首先要了解一些背景知识:我有一个WPF应用程序,它是旧版Win32应用程序的GUI前端.旧版应用程序在单独的线程中作为DLL运行.用户在UI中选择的命令将在该旧线程"上调用.
First a bit of background: I have a WPF application, which is a GUI-front-end to a legacy Win32-application. The legacy app runs as DLL in a separate thread. The commands the user chooses in the UI are invoked on that "legacy thread".
如果旧版线程"完成,则GUI前端将无法再执行任何有用的操作,因此我需要关闭WPF应用程序.因此,在线程方法的末尾,我调用Application.Current.Shutdown()
.
If the "legacy thread" finishes, the GUI-front-end cannot do anything useful anymore, so I need to shutdown the WPF-application. Therefore, at the end of the thread's method, I call Application.Current.Shutdown()
.
由于我不在主线程上,因此需要调用此命令.但是,然后我注意到Dispatcher也有BeginInvokeShutdown()
来关闭调度程序.所以我的问题是:调用之间有什么区别
Since I am not on the main thread, I need to invoke this command. However, then I noticed that the Dispatcher also has BeginInvokeShutdown()
to shutdown the dispatcher. So my question is: What is the difference between invoking
Application.Current.Shutdown();
并致电
Application.Current.Dispatcher.BeginInvokeShutdown();
推荐答案
我进行了更多测试,现在我想我知道其中的区别:
I did some more testing, and now I think I know the differences:
1)如MSDN页面中所述,BeginInvokeShutdown
除了关闭Dispatcher之外,还清除/中止其队列. Shutdown
首先处理Dispatcher队列中的所有项目.
1) As stated in the MSDN page, BeginInvokeShutdown
, besides shutting down the Dispatcher, also clears/aborts its queue. Shutdown
first handles all items in the Dispatcher queue.
2)在应用程序中,我可以处理应用程序.Exit 事件.当我调用Shutdown时会触发此事件,但在我调用BeginInvokeShutdown时不会触发该事件! Window.Closing 和 Window.Closed .
2) In an application I can handle the Application.Exit event. This event is fired when I call Shutdown, but NOT fired when I call BeginInvokeShutdown! The same applies to Window.Closing and Window.Closed.
关于相似性,在两种情况下都退出了主线程.根据其他正在运行的线程,这也会关闭进程:非后台线程在进程退出之前运行完毕.
As for similarities, in both cases the main thread is exited. Depending on other running threads, this also shuts down the process: non-background threads are run to completion before the process exits.
下面是我的测试代码.注释Application_Startup中的一个或另一个方法调用:
Below is my test code. Comment one or the other method call in Application_Startup:
public partial class App
{
private void Application_Exit(object sender, ExitEventArgs e)
{
MessageBox.Show("Exiting");
}
private void Application_Startup(object sender, StartupEventArgs e)
{
var testThread = new Thread(
() =>
{
Thread.Sleep(2000);
Application.Current.Dispatcher.BeginInvokeShutdown(System.Windows.Threading.DispatcherPriority.Send);
//Application.Current.Dispatcher.BeginInvoke(new Action(() => Application.Current.Shutdown()));
});
testThread.Start();
}
}
public partial class Window1
{
public Window1()
{
this.InitializeComponent();
Dispatcher.BeginInvoke(new Action(() =>
{
Thread.Sleep(1000);
Console.WriteLine("One");
}));
Dispatcher.BeginInvoke(new Action(() =>
{
Thread.Sleep(1000);
Console.WriteLine("Two");
}));
Dispatcher.BeginInvoke(new Action(() =>
{
Thread.Sleep(1000);
Console.WriteLine("Three");
}));
Dispatcher.BeginInvoke(new Action(() =>
{
Thread.Sleep(1000);
Console.WriteLine("Four");
}));
}
private void Window_Closed(object sender, EventArgs e)
{
Console.WriteLine("Closed");
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
Console.WriteLine("Closing");
}
}
这篇关于Application.Current.Shutdown()与Application.Current.Dispatcher.BeginInvokeShutdown()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!