Application.Current.Shutdown() vs. Application.Current.Dispatcher.BeginInvokeShutdown()

后端 未结 3 778
灰色年华
灰色年华 2020-12-30 11:09

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 th

相关标签:
3条回答
  • 2020-12-30 11:31

    I did some more testing, and now I think I know the differences:

    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.

    Once the shutdown process begins, all pending work items in the queue are aborted.

    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.

    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");
        }
    }
    
    0 讨论(0)
  • 2020-12-30 11:35

    By the way, Daniel - our application is built in the same way - WPF front-end to a Win32 COM (Delphi) application running separately. It would be interesting to talk and compare our approaches.

    But why I have stumbled upon your question is that our application used to call Dispatcher.BeginInvokeShutdown, which worked fine with exception of a few cases where it did not terminate properly. When I finally got my hands on a machine on which this was reproducible, switching to Application.Current.Shutdown() fixed the problem. I still do not understand what was the problem.

    0 讨论(0)
  • 2020-12-30 11:41

    MSDN page for Shutdown()
    MSDN page for BeginInvokeShutdown()

    It seems that this is just a collision of terminology. BeginInvokeShutdown shuts down that dispatcher, your application can in theory continue to live afterwards (though without a dispatcher, it'd be pretty limited). Shutdown however actually exits your application, which is what you want.

    0 讨论(0)
提交回复
热议问题