Is there a way to perform a time-consuming exit process in MAUI without freezing?

Viewed 79

I am looking for a way to handle termination in MAUI and I have learned that I can write the termination process in the Destroying event I found out that I can write the exit processing in the Destroying event written in the App lifecycle - .NET MAUI | Microsoft Docs. However, if I write a time-consuming exit process like the code below, the application freezes just before exiting.

To run a time-consuming process in a separate thread await Task.Delay(5000);, the application process would be terminated before the task was finished, so The termination process could not be completed.

How can I run a time-consuming exit process in MAUI without freezing?

Target platforms are expected to be Windows and Android.

Thanks.

App.xaml.cs

using System.Diagnostics;

public partial class App : Application
{
    public App()
    {
        InitializeComponent();
        MainPage = new AppShell();
    }

    protected override Window CreateWindow(IActivationState? activationState)
    {
        var window = base.CreateWindow(activationState);
        window.Destroying += (s, e) => 
        {
            Task.Delay(5000).Wait(); // Long time end process.
            Debug.WriteLine("Time-consuming termination process is completed");
        };

        return window;
    }
}

development environment

  • Visual Studio2022
  • Windows11
0 Answers
Related