开发者

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

开发者 https://www.devze.com 2023-02-12 16:47 出处:网络
I am writing some code that checks that my resources are properly cleaned up. When the application is shutdown, resources are not c开发者_StackOverflow社区leaned up, which is fine. However, this make

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

When the application is shutdown, resources are not c开发者_StackOverflow社区leaned up, which is fine. However, this makes my check code fail.

Is there a way to know if a WPF application is in the process of shutting down? - Something like Application.Current.IsShuttingDown?


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.


    /// <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;
        }
    }


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

精彩评论

暂无评论...
验证码 换一张
取 消