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 : IDisposable

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);
}
Implements

Constructors

NameDescription
SingleInstance(Guid)Ensures that only a single instance of an application can run at a time and provides communication between instances.

SingleInstance(Guid)

SingleInstance.cs

Ensures that only a single instance of an application can run at a time and provides communication between instances.

public SingleInstance(Guid applicationId)

Parameters

applicationId Guid

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

NameDescription
ClientConnectionTimeoutGets or sets the timeout for connecting to the first instance when notifying it.
StartServerGets or sets a value indicating whether to start a named pipe server to receive notifications from other instances.

ClientConnectionTimeout

SingleInstance.cs

Gets or sets the timeout for connecting to the first instance when notifying it.

public TimeSpan ClientConnectionTimeout { get; set; }

Property value

TimeSpan The connection timeout. The default is 3 seconds.

Gets or sets a value indicating whether to start a named pipe server to receive notifications from other instances.

public bool StartServer { get; set; }

Property value

bool true to start the server; otherwise, false. The default is true.

Methods

NameDescription
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.

Releases all resources used by the SingleInstance object.

public void Dispose()

NotifyFirstInstance(string[])

SingleInstance.cs

Notifies the first instance of the application that another instance is attempting to start.

public bool NotifyFirstInstance(string[] args)

Parameters

args string[]
The command-line arguments to send to the first instance.

Returns

bool true if the notification was sent successfully; otherwise, false.

Exceptions

ArgumentNullException
args is null.

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.cs

Attempts 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

NameDescription
NewInstanceOccurs 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