ktsu.SingleAppInstance 1.3.1
ktsu.SingleAppInstance
A .NET library that ensures only one instance of your application is running at a time.
Introduction
ktsu.SingleAppInstance is a lightweight .NET library that provides a robust mechanism to ensure only one instance of an application is running at a time. It uses a JSON-serialized PID file with multi-attribute process verification to accurately detect running instances, making it ideal for desktop applications, services, or any software that requires instance exclusivity to prevent resource conflicts or maintain data integrity.
Features
- Single Instance Enforcement: Prevents multiple copies of your application from running simultaneously
- Enhanced Process Identification: Verifies running instances using multiple attributes (PID, process name, start time, executable path) for accurate detection
- Race Condition Handling: Includes a built-in 1-second delay to safely detect simultaneous startup attempts
- PID File Management: Stores process information as JSON in the application data directory
- Backward Compatibility: Gracefully handles legacy PID files that stored only a plain integer PID
- Simple API: Two methods —
ExitIfAlreadyRunning()for automatic exit andShouldLaunch()for custom logic - Multi-Target Support: Works across .NET 10.0 through .NET 5.0, .NET Standard 2.0/2.1
Installation
Package Manager Console
Install-Package ktsu.SingleAppInstance
.NET CLI
dotnet add package ktsu.SingleAppInstance
Package Reference
<PackageReference Include="ktsu.SingleAppInstance" Version="x.y.z" />
Usage Examples
Basic Example
The simplest way to use SingleAppInstance is to call ExitIfAlreadyRunning at the start of your application. If another instance is detected, the process exits automatically:
using ktsu.SingleAppInstance;
class Program
{
static void Main(string[] args)
{
SingleAppInstance.ExitIfAlreadyRunning();
// Your application code here
Console.WriteLine("Application is running.");
}
}
Custom Launch Logic
If you prefer to handle the duplicate-instance case yourself, use ShouldLaunch() which returns a boolean:
using ktsu.SingleAppInstance;
class Program
{
static void Main(string[] args)
{
if (SingleAppInstance.ShouldLaunch())
{
// Your application code here
Console.WriteLine("Application is running.");
}
else
{
Console.WriteLine("Another instance is already running.");
}
}
}
WPF Application Integration
using System.Windows;
using ktsu.SingleAppInstance;
namespace MyWpfApp
{
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
if (!SingleAppInstance.ShouldLaunch())
{
MessageBox.Show("Application is already running.");
Shutdown();
return;
}
MainWindow = new MainWindow();
MainWindow.Show();
}
}
}
API Reference
SingleAppInstance
A static class that provides single-instance enforcement for applications. Uses a PID file stored in the application data directory to track running instances.
Methods
| Name | Return Type | Description |
|---|---|---|
ExitIfAlreadyRunning() |
void |
Checks if another instance is running and calls Environment.Exit(0) if so |
ShouldLaunch() |
bool |
Writes a PID file, waits 1 second for race condition detection, and returns true if safe to proceed |
Contributing
Contributions are welcome! Feel free to open issues or submit pull requests.
License
This project is licensed under the MIT License. See the LICENSE.md file for details.
No packages depend on ktsu.SingleAppInstance.
.NET 5.0
- Polyfill (>= 9.10.0)
- ktsu.AppDataStorage (>= 1.16.0)
- ktsu.Semantics.Paths (>= 1.0.31)
.NET Standard 2.1
- ktsu.Semantics.Paths (>= 1.0.31)
- ktsu.AppDataStorage (>= 1.16.0)
- Polyfill (>= 9.10.0)
.NET Standard 2.0
- ktsu.Semantics.Paths (>= 1.0.31)
- ktsu.AppDataStorage (>= 1.16.0)
- Polyfill (>= 9.10.0)
.NET 10.0
- ktsu.Semantics.Paths (>= 1.0.31)
- Polyfill (>= 9.10.0)
- ktsu.AppDataStorage (>= 1.16.0)
.NET 9.0
- ktsu.Semantics.Paths (>= 1.0.31)
- ktsu.AppDataStorage (>= 1.16.0)
- Polyfill (>= 9.10.0)
.NET 8.0
- ktsu.AppDataStorage (>= 1.16.0)
- ktsu.Semantics.Paths (>= 1.0.31)
- Polyfill (>= 9.10.0)
.NET 6.0
- Polyfill (>= 9.10.0)
- ktsu.AppDataStorage (>= 1.16.0)
- ktsu.Semantics.Paths (>= 1.0.31)
.NET 7.0
- ktsu.AppDataStorage (>= 1.16.0)
- ktsu.Semantics.Paths (>= 1.0.31)
- Polyfill (>= 9.10.0)
| Version | Downloads | Last updated |
|---|---|---|
| 1.3.13 | 0 | 04/13/2026 |
| 1.3.12 | 1 | 04/10/2026 |
| 1.3.11 | 1 | 04/09/2026 |
| 1.3.10 | 1 | 04/08/2026 |
| 1.3.9 | 1 | 04/07/2026 |
| 1.3.8 | 1 | 04/06/2026 |
| 1.3.7 | 0 | 04/03/2026 |
| 1.3.6 | 1 | 04/02/2026 |
| 1.3.5 | 1 | 02/27/2026 |
| 1.3.4 | 16 | 02/26/2026 |
| 1.3.3 | 37 | 02/25/2026 |
| 1.3.2 | 38 | 02/24/2026 |
| 1.3.1 | 51 | 02/23/2026 |
| 1.3.0 | 54 | 02/20/2026 |
| 1.2.17 | 54 | 02/19/2026 |
| 1.2.16 | 55 | 02/18/2026 |
| 1.2.15 | 54 | 02/17/2026 |
| 1.2.15-pre.1 | 53 | 02/17/2026 |
| 1.2.14 | 53 | 02/16/2026 |
| 1.2.14-pre.1 | 54 | 02/16/2026 |
| 1.2.13 | 54 | 02/14/2026 |
| 1.2.12 | 53 | 02/14/2026 |
| 1.2.12-pre.12 | 57 | 02/06/2026 |
| 1.2.12-pre.11 | 57 | 02/06/2026 |
| 1.2.12-pre.10 | 60 | 02/05/2026 |
| 1.2.12-pre.9 | 57 | 02/04/2026 |
| 1.2.12-pre.8 | 57 | 02/03/2026 |
| 1.2.12-pre.7 | 59 | 02/03/2026 |
| 1.2.12-pre.6 | 59 | 02/02/2026 |
| 1.2.12-pre.5 | 59 | 02/01/2026 |
| 1.2.12-pre.4 | 60 | 01/31/2026 |
| 1.2.12-pre.3 | 60 | 01/31/2026 |
| 1.2.12-pre.2 | 61 | 01/31/2026 |
| 1.2.12-pre.1 | 59 | 01/30/2026 |
| 1.2.11 | 61 | 01/30/2026 |
| 1.2.11-pre.2 | 61 | 01/29/2026 |
| 1.2.11-pre.1 | 59 | 01/28/2026 |
| 1.2.10 | 62 | 01/28/2026 |
| 1.2.9 | 75 | 01/13/2026 |
| 1.2.8 | 75 | 01/12/2026 |
| 1.2.8-pre.1 | 88 | 01/08/2026 |
| 1.2.7 | 88 | 01/08/2026 |
| 1.2.7-pre.4 | 145 | 11/24/2025 |
| 1.2.7-pre.3 | 138 | 11/23/2025 |
| 1.2.7-pre.2 | 137 | 11/23/2025 |
| 1.2.7-pre.1 | 137 | 11/23/2025 |
| 1.2.6 | 140 | 08/25/2025 |