Vey.SafeStop 0.0.2

dotnet add package Vey.SafeStop --version 0.0.2
                    
NuGet\Install-Package Vey.SafeStop -Version 0.0.2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Vey.SafeStop" Version="0.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Vey.SafeStop" Version="0.0.2" />
                    
Directory.Packages.props
<PackageReference Include="Vey.SafeStop" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Vey.SafeStop --version 0.0.2
                    
#r "nuget: Vey.SafeStop, 0.0.2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=Vey.SafeStop&version=0.0.2
                    
Install Vey.SafeStop as a Cake Addin
#tool nuget:?package=Vey.SafeStop&version=0.0.2
                    
Install Vey.SafeStop as a Cake Tool

SafeStop

A class for managing the safe shutdown of an application

Allows the creation of critical sections that cannot be interrupted by the application stop signal.

API

Critical Sections

  • Enter a critical section using the CriticalAsync() or Critical() methods.
  • These methods return a DisposeAction. To release the critical section, simply use the using statement or call the Release() method.

Stopping

  • To request a stop, simply call Stop(). This action cannot be undone.
  • After calling Stop(), use WaitStopAsync() or WaitStop() to wait for all critical sections to complete.

Usage Example

var safeStop = new SafeStopManager() {
    // If set, an exception will be thrown when entering critical sections after the stop signal is set.
    // Otherwise, it will wait indefinitely.
    enterAfterStopException = new InvalidOperationException("Cannot enter critical section, stop signal is set")
};

// Handling the Ctrl+C signal
Console.CancelKeyPress += (sender, eventArgs) => {
    eventArgs.Cancel = true;
    safeStop.Stop();
    Console.WriteLine("Stop signal received. Shutting down...");
};

// Running a background task
_ = Task.Run(async () => {
    try {
        // The section will automatically be released when using the 'using' statement
        using var critical = await safeStop.CriticalAsync();
        Console.WriteLine("Executing critical task in the background...");
        await Task.Delay(5000); // Simulate work
        Console.WriteLine("Background task completed.");
        critical.Release(); // Manual release of the critical section is also possible
    } catch (Exception ex) {
        Console.WriteLine($"Error: {ex.Message}");
    }
});

// Waiting for all critical sections to complete before shutting down
await safeStop.WaitStopAsync();
Console.WriteLine("All critical tasks completed. The application can be safely stopped.");
Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net9.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Vey.SafeStop:

Package Downloads
Telegram.Bot.UI

Convenient creation and localization of bots in Telegram

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.0.2 40 5/12/2025
0.0.1 128 2/5/2025