ktsu.SingleAppInstance
1.2.3
Prefix Reserved
See the version list below for details.
dotnet add package ktsu.SingleAppInstance --version 1.2.3
NuGet\Install-Package ktsu.SingleAppInstance -Version 1.2.3
<PackageReference Include="ktsu.SingleAppInstance" Version="1.2.3" />
<PackageVersion Include="ktsu.SingleAppInstance" Version="1.2.3" />
<PackageReference Include="ktsu.SingleAppInstance" />
paket add ktsu.SingleAppInstance --version 1.2.3
#r "nuget: ktsu.SingleAppInstance, 1.2.3"
#addin nuget:?package=ktsu.SingleAppInstance&version=1.2.3
#tool nuget:?package=ktsu.SingleAppInstance&version=1.2.3
ktsu.SingleAppInstance
Overview
The SingleAppInstance
class provides a mechanism to ensure that only one instance of an application is running at a time. This is useful for applications that should not have multiple instances running simultaneously, such as desktop applications or services.
Features
- Single Instance Enforcement: Ensures that only one instance of the application is running.
- Enhanced Process Identification: Uses detailed process information (PID, process name, start time, main module filename) to accurately identify running instances.
- PID File Management: Stores process information in JSON format in the application data directory.
- Race Condition Handling: Handles potential race conditions when starting multiple instances simultaneously with a timeout mechanism.
- Backward Compatibility: Maintains compatibility with older versions that only stored the PID.
Installation
To install the ktsu.SingleAppInstance
package, run the following command:
dotnet add package ktsu.SingleAppInstance
Usage
Basic Usage
To use the SingleAppInstance
class, call the ExitIfAlreadyRunning
method at the start of your application:
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 explicitly handle the case where another instance is already running:
using ktsu.SingleAppInstance;
class Program
{
static void Main(string[] args)
{
if (SingleAppInstance.ShouldLaunch())
{
// Your application code here
Console.WriteLine("Application is running.");
}
else
{
// Handle the case where another instance is already running
Console.WriteLine("Another instance is already running.");
}
}
}
Methods
ExitIfAlreadyRunning()
: Exits the application with a status code of 0 if another instance is already running.ShouldLaunch()
: Determines whether the application should launch based on whether another instance is running. It also handles writing the current process information to a PID file and implements a waiting mechanism to handle potential race conditions.
Technical Implementation
Under the hood, SingleAppInstance:
Stores a JSON-serialized
ProcessInfo
object containing:- Process ID
- Process name
- Start time
- Main module filename
Uses the application data directory to store the PID file. The file is named
.SingleAppInstance.pid
.When checking for running instances, it:
- Reads the PID file
- Verifies if a process with the stored ID exists
- Confirms it's the same application by comparing process name and executable path
- Handles access restrictions and various edge cases
Implements a 1-second timeout in
ShouldLaunch()
to detect race conditions when multiple instances start simultaneously.
Contributing
Contributions are welcome! Please open an issue or submit a pull request on GitHub.
Support
If you encounter any issues or have questions, please open an issue on GitHub.
License
This project is licensed under the MIT License.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. 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. |
-
net8.0
- ktsu.AppDataStorage (>= 1.7.2)
-
net9.0
- ktsu.AppDataStorage (>= 1.7.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last Updated |
---|---|---|
1.2.6-pre.18 | 116 | 5/20/2025 |
1.2.6-pre.15 | 73 | 5/17/2025 |
1.2.6-pre.14 | 127 | 5/16/2025 |
1.2.6-pre.13 | 198 | 5/15/2025 |
1.2.6-pre.12 | 194 | 5/14/2025 |
1.2.6-pre.11 | 194 | 5/13/2025 |
1.2.6-pre.10 | 227 | 5/12/2025 |
1.2.6-pre.9 | 164 | 5/11/2025 |
1.2.6-pre.8 | 104 | 5/10/2025 |
1.2.6-pre.7 | 46 | 5/9/2025 |
1.2.6-pre.6 | 117 | 5/8/2025 |
1.2.6-pre.5 | 117 | 5/7/2025 |
1.2.6-pre.4 | 114 | 5/6/2025 |
1.2.6-pre.3 | 114 | 5/5/2025 |
1.2.6-pre.2 | 116 | 5/4/2025 |
1.2.6-pre.1 | 111 | 5/4/2025 |
1.2.5 | 145 | 5/4/2025 |
1.2.5-pre.3 | 107 | 4/29/2025 |
1.2.5-pre.2 | 128 | 4/28/2025 |
1.2.5-pre.1 | 62 | 4/26/2025 |
1.2.4 | 141 | 4/25/2025 |
1.2.3 | 111 | 4/19/2025 |
1.2.2 | 176 | 4/18/2025 |
1.2.2-pre.3 | 60 | 4/4/2025 |
1.2.2-pre.2 | 113 | 4/4/2025 |
1.2.2-pre.1 | 132 | 3/31/2025 |
1.2.1 | 147 | 3/30/2025 |
1.2.0 | 149 | 3/30/2025 |
1.1.1-pre.2 | 74 | 3/29/2025 |
1.1.1-pre.1 | 462 | 3/25/2025 |
1.1.0 | 119 | 2/17/2025 |
1.0.0-pre.19 | 71 | 2/14/2025 |
1.0.0-pre.18 | 72 | 2/6/2025 |
1.0.0-pre.17 | 68 | 2/5/2025 |
1.0.0-pre.16 | 61 | 2/5/2025 |
1.0.0-pre.15 | 69 | 2/3/2025 |
1.0.0-pre.14 | 70 | 2/3/2025 |
1.0.0-pre.13 | 62 | 2/3/2025 |
1.0.0-pre.12 | 79 | 2/2/2025 |
1.0.0-pre.11 | 65 | 1/31/2025 |
1.0.0-pre.10 | 63 | 1/30/2025 |
1.0.0-pre.9 | 75 | 1/28/2025 |
1.0.0-pre.8 | 64 | 1/26/2025 |
1.0.0-pre.7 | 58 | 1/24/2025 |
1.0.0-pre.6 | 65 | 1/22/2025 |
1.0.0-pre.5 | 65 | 1/20/2025 |
1.0.0-pre.4 | 57 | 1/18/2025 |
1.0.0-pre.3 | 58 | 1/16/2025 |
1.0.0-pre.2 | 43 | 1/14/2025 |
1.0.0-pre.1 | 65 | 1/13/2025 |
0.0.1-pre.1 | 58 | 1/13/2025 |
## v1.2.3 (patch)
Changes since v1.2.2:
- Add guidelines for using command line in non-interactive mode and directory context ([@matt-edmondson](https://github.com/matt-edmondson))
- Add memory.jsonl file with project and class details ([@matt-edmondson](https://github.com/matt-edmondson))
- Add SingleAppInstance.Test project to solution and configure build settings ([@matt-edmondson](https://github.com/matt-edmondson))
- Add tests for SingleAppInstance behavior with no other instance running and custom launch logic ([@matt-edmondson](https://github.com/matt-edmondson))
- Add workflow and process guidelines to Copilot instructions ([@matt-edmondson](https://github.com/matt-edmondson))
- Clarify command line usage by providing an example for non-interactive mode with `git` commands ([@matt-edmondson](https://github.com/matt-edmondson))
- Comment out MaxCpuCount setting in .runsettings to disable process-level parallelization ([@matt-edmondson](https://github.com/matt-edmondson))
- Enhance memory.jsonl to include additional details for SingleAppInstance tests and project structure ([@matt-edmondson](https://github.com/matt-edmondson))
- Enhance memory.jsonl to include advanced process detection and compatibility details for SingleAppInstance class ([@matt-edmondson](https://github.com/matt-edmondson))
- Enhance SingleAppInstance to store and verify process information in JSON format ([@matt-edmondson](https://github.com/matt-edmondson))
- Enhance SingleAppInstanceTests by adding assertions to verify PID file handling and initial state checks ([@matt-edmondson](https://github.com/matt-edmondson))
- Organize and enhance Copilot documentation with detailed guidelines on memory usage, coding standards, and project management practices. ([@matt-edmondson](https://github.com/matt-edmondson))
- Refactor SingleAppInstanceTests for better isolation ([@matt-edmondson](https://github.com/matt-edmondson))
- Refactor SingleAppInstanceTests to improve clarity and structure of test methods, ensuring accurate PID file handling and process information validation. ([@matt-edmondson](https://github.com/matt-edmondson))
- Refactor SingleAppInstanceTests to simplify file deletion logic and remove unnecessary try-finally blocks, ensuring clearer test flow and improved readability. ([@matt-edmondson](https://github.com/matt-edmondson))
- Refine README.md for clarity and usage instructions of SingleAppInstance ([@matt-edmondson](https://github.com/matt-edmondson))
- Refine README.md to enhance feature descriptions and clarify technical implementation details for SingleAppInstance ([@matt-edmondson](https://github.com/matt-edmondson))
- Refine workflow guidelines for clarity on specialized tool usage and documentation checks ([@matt-edmondson](https://github.com/matt-edmondson))
- Set max cpus for tests to 1 ([@matt-edmondson](https://github.com/matt-edmondson))
- Update Copilot instructions for .NET project build and testing guidelines ([@matt-edmondson](https://github.com/matt-edmondson))
- Update language-specific guidelines to clarify test execution commands and improve resource management ([@matt-edmondson](https://github.com/matt-edmondson))
- Update language-specific guidelines to clarify tool usage and fallback options ([@matt-edmondson](https://github.com/matt-edmondson))
- Update test command to run in a single process ([@matt-edmondson](https://github.com/matt-edmondson))
## v1.2.2 (patch)
Changes since v1.2.1:
- Add comprehensive Copilot instructions and memory management guidelines ([@matt-edmondson](https://github.com/matt-edmondson))
- Add markdownlint configuration file for linting rules ([@matt-edmondson](https://github.com/matt-edmondson))
## v1.2.1 (patch)
Changes since v1.2.0:
- Update packages ([@matt-edmondson](https://github.com/matt-edmondson))
## v1.2.0 (minor)
Changes since v1.1.0:
- Add LICENSE template ([@matt-edmondson](https://github.com/matt-edmondson))
## v1.1.0 (minor)
Changes since v1.0.0-pre.19:
- Apply new editorconfig ([@matt-edmondson](https://github.com/matt-edmondson))