Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle system shutdown event using Avalonia framework on both Windows and Linux systems?

I'm developing an application using C# with Avalonia framework. When system shuts down I need to perform some tasks (write logs and metadata to files) for application to complete job gracefully. Previously using WPF on Windows I used subscription to Microsoft.Win32.SystemEvents.SessionEnding event. But as namespace says, it is OS dependent. In Avalonia I tried to handle AppDomain.CurrentDomain.ProcessExit or ((IClassicDesktopStyleApplicationLifetime)ApplicationLifetime).Exit events but neither of them fired. So, question is, what is the correct way to handle OS shutdown on both systems.

like image 698
Yuri Plokhih Avatar asked Oct 27 '25 20:10

Yuri Plokhih


1 Answers

You should try the shutdown event, it's signature is as follows...

((IClassicDesktopStyleApplicationLifetime)ApplicationLifetime).ShutdownRequested += delegate(object? sender, ShutdownRequestedEventArgs e) 
{
    //Write logs and metadata to files here...
};
like image 112
Lyle February Avatar answered Oct 30 '25 12:10

Lyle February