Is there a way to know if a WPF application is shutting down?

前端 未结 3 1191
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-20 16:53

I am writing some code that checks that my resources are properly cleaned up.

When the application is shutdown, resources are not cleaned up, which is fine. However, thi

相关标签:
3条回答
  • 2021-02-20 17:35

    just add this to your App.cs file

        public bool IsShuttingDown { get; private set; }
    
        public new void Shutdown(int exitCode = 0)
        {
            this.IsShuttingDown = true;
            base.Shutdown(exitCode);
        }
    
    0 讨论(0)
  • 2021-02-20 17:39
        /// <summary>
        /// Hack to check if the application is shutting down.
        /// </summary>
        public static bool IsShuttingDown()
        {
            try
            {
                Application.Current.ShutdownMode = Application.Current.ShutdownMode;
                return false;
            }
            catch (Exception)
            {
                return true;
            }
        }
    
    0 讨论(0)
  • 2021-02-20 17:48

    There is Application.Exit event, you should be able to do with that.

    If you really need it to be a property, then create a property into your App class (your class inheriting Windows.Application) and set it to true in with the Application.Exit event.

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