This library provides a scheduler for automatically saving user data and a supervisor process to automatically restart your app after a crash.
$ dotnet add package Abraham.AutoRecoveryThis library provides a scheduler for automatically saving user data and a supervisor process to automatically restart your app after a crash.
Licensed under Apache licence. https://www.apache.org/licenses/LICENSE-2.0
The nuget package was build with DotNET 6.
For an example refer to project "AutoRecoveryDemoWPF". It demonstrates both features:
Add the Nuget package "Abraham.AutoRecovery" to your project.
Add a field to your project:
IAutoRecovery _autoRecovery = new AutoRecovery();
At startup of your app, add the following code:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
if (_autoRecovery.AppHasCrashed)
LoadDataFromAutoSaveFile();
else
LoadData();
_autoRecovery.EnableAutoSave(10, SaveDataForAutoSave); // save every 10 seconds
}
This method will be called periodically to save the user's data:
private void SaveDataForAutoSave()
{
Dispatcher.Invoke(() => { SaveDataToAutoSaveFile(); });
}
At shutdown, add the following code:
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
SaveData();
DeleteAutoSaveFile(); // optional
}
After saving user's data as usual, you can add a method that deletes the file containing the autosave data.
At startup of your app, add the following code. (You'll want to change startVisible to false after verifying it's working):
private void Window_Loaded(object sender, RoutedEventArgs e)
{
_autoRecovery.EnableAutoRestart(startVisible: true); // restart this app after crash, start visible for Demo only!
}
At shutdown, add the following code:
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
_autoRecovery.NormalShutdown();
}
This call will end the hidden supervisor process.
This is very simple:
or from NuGet Command-Line:
Install-Package Abraham.ProgramSettingsManager
Oliver Abraham, mail@oliver-abraham.de, https://www.oliver-abraham.de
Please feel free to comment and suggest improvements!