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