Share via

Write query when closing the app

Dani_S 5,581 Reputation points
2023-08-10T07:01:21.9866667+00:00

Hi,

I'm in net 7, I want the close event of app button and write query of http to check something, how is can be done ?

Thanks,

Developer technologies | .NET | .NET Multi-platform App UI

Answer accepted by question author

Anonymous
2023-08-11T02:57:35.97+00:00

Hello,

I need the close event of the application ?

Yes, if you want to detect the close button event in the title bar, you can get the appwindow for windows platform. And re-write the close event, after query operation finished, then close this application.

 public static MauiApp CreateMauiApp()
    {
        var builder = MauiApp.CreateBuilder();
        builder
            .UseMauiApp<App>()
            .ConfigureLifecycleEvents(events =>
            {
               //add here

#if WINDOWS
events.AddWindows(windowsLifecycleBuilder =>
{
    windowsLifecycleBuilder.OnWindowCreated(window =>
    {        
        var handle = WinRT.Interop.WindowNative.GetWindowHandle(window);
        var id = Microsoft.UI.Win32Interop.GetWindowIdFromWindow(handle);
        var appWindow = Microsoft.UI.Windowing.AppWindow.GetFromWindowId(id);      
        appWindow.Closing += async (s, e) =>
        {
            e.Cancel = true;
            using HttpClient httpClient = new HttpClient();
           // Compose and send the HTTP request
            HttpResponseMessage responseCode = await httpClient.GetAsync("https://learn.microsoft.com/");
            // Check the response status
            if (responseCode.IsSuccessStatusCode)
            {
               // "query successful";
                App.Current.Quit();
            }
            else
            {
              // query failed"
                App.Current.Quit();
            }
                       
           
        };
    });
});
#endif

Best Regards,

Leon Lu


If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

Was this answer helpful?

1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Vahid Ghafarpour 23,605 Reputation points
    2023-08-10T07:10:21.57+00:00

    I hope this sample code help you:

    using System;
    using System.Net.Http;
    using System.Threading.Tasks;
    
    class Program
    {
        static async Task Main(string[] args)
        {
            // Simulate a button close event
            await HandleButtonCloseEvent();
    
            // Your application's cleanup code
        }
    
        static async Task HandleButtonCloseEvent()
        {
            using HttpClient httpClient = new HttpClient();
    
            // Compose and send the HTTP request
            HttpResponseMessage response = await httpClient.GetAsync("https://api.example.com/some_endpoint");
    
            // Check the response status
            if (response.IsSuccessStatusCode)
            {
                Console.WriteLine("HTTP query successful");
            }
            else
            {
                Console.WriteLine("HTTP query failed");
            }
        }
    }
    
    

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.