有没有办法知道WPF应用程序是否正在关闭?

时间:2011-02-21 20:18:38

标签: wpf

我正在编写一些代码来检查我的资源是否已正确清理。

当应用程序关闭时,资源不会被清除,这很好。但是,这使我的检查代码失败。

有没有办法知道WPF应用程序是否正在关闭? - 像Application.Current.IsShuttingDown?

3 个答案:

答案 0 :(得分:7)

Application.Exit事件,您应该能够做到这一点。

如果您真的需要它作为属性,那么在App类中创建一个属性(您的类继承Windows.Application)并使用Application.Exit事件将其设置为true。

答案 1 :(得分:0)

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

答案 2 :(得分:0)

只需将其添加到您的App.cs文件中

S C:\aspnetcore-docker> dotnet --list-runtimes
Microsoft.AspNetCore.All 2.1.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.App 2.1.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]