SingleInstance Class
Ensures that only a single instance of an application can run at a time and provides communication between instances.
public sealed class SingleInstance : IDisposableExample
Basic usage:
var applicationId = new Guid("dfae4e70-179f-4726-aa98-00a832315f5a");
using var singleInstance = new SingleInstance(applicationId);
// Subscribe before calling StartApplication, otherwise a notification raised
// between the server starting and the handler being attached is dropped.
singleInstance.NewInstance += (sender, e) =>
{
// Handle notification from another instance
Console.WriteLine($"New instance started with {e.Arguments.Length} arguments");
};
if (singleInstance.StartApplication())
{
// This is the first instance
}
else
{
// Notify the first instance
singleInstance.NotifyFirstInstance(args);
}- Implements
Constructors
| Name | Description |
|---|---|
| SingleInstance(Guid) | Ensures that only a single instance of an application can run at a time and provides communication between instances. |
SingleInstance(Guid)
SingleInstance.csEnsures that only a single instance of an application can run at a time and provides communication between instances.
public SingleInstance(Guid applicationId)Parameters
applicationIdGuid
Example
Basic usage:
var applicationId = new Guid("dfae4e70-179f-4726-aa98-00a832315f5a");
using var singleInstance = new SingleInstance(applicationId);
// Subscribe before calling StartApplication, otherwise a notification raised
// between the server starting and the handler being attached is dropped.
singleInstance.NewInstance += (sender, e) =>
{
// Handle notification from another instance
Console.WriteLine($"New instance started with {e.Arguments.Length} arguments");
};
if (singleInstance.StartApplication())
{
// This is the first instance
}
else
{
// Notify the first instance
singleInstance.NotifyFirstInstance(args);
}Properties
| Name | Description |
|---|---|
| ClientConnectionTimeout | Gets or sets the timeout for connecting to the first instance when notifying it. |
| StartServer | Gets or sets a value indicating whether to start a named pipe server to receive notifications from other instances. |
ClientConnectionTimeout
SingleInstance.csStartServer
SingleInstance.csMethods
| Name | Description |
|---|---|
| Dispose() | Releases all resources used by the SingleInstance object. |
| NotifyFirstInstance(string[]) | Notifies the first instance of the application that another instance is attempting to start. |
| StartApplication() | Attempts to start the application as the first instance. |
Dispose()
SingleInstance.csReleases all resources used by the SingleInstance object.
public void Dispose()NotifyFirstInstance(string[])
SingleInstance.csNotifies the first instance of the application that another instance is attempting to start.
Parameters
argsstring[]- The command-line arguments to send to the first instance.
Returns
bool true if the notification was sent successfully; otherwise, false.
Exceptions
ArgumentNullExceptionargsisnull.
Remarks
The first instance must have SingleInstance.StartServer set to true to receive notifications. The method will timeout after SingleInstance.ClientConnectionTimeout if the first instance is not responding. Failing to reach the first instance is reported by returning false instead of throwing.
StartApplication()
SingleInstance.csAttempts to start the application as the first instance.
public bool StartApplication()Returns
bool true if this is the first instance and the application can start; otherwise, false.
Remarks
If this method returns true, the application should continue running and can receive notifications from other instances through the SingleInstance.NewInstance event. If this method returns false, the application should call SingleInstance.NotifyFirstInstance to notify the first instance and then exit. The instance is scoped to the current user: two users of the same machine can each run their own instance.
Events
| Name | Description |
|---|---|
| NewInstance | Occurs when another instance of the application attempts to start. |
NewInstance
Occurs when another instance of the application attempts to start.
public event EventHandler<SingleInstanceEventArgs>? NewInstance