Skip to content

Single Instance ​

Work in Progress

This guide is under development. Check back soon!

Overview ​

Hermes provides single-instance support to ensure only one copy of your application runs at a time. When a second instance launches, its command-line arguments are forwarded to the first instance via named pipes, then the second instance exits.

Quick Reference ​

With Blazor ​

csharp
var builder = HermesBlazorAppBuilder.CreateDefault(args);

builder.SingleInstance("com.mycompany.myapp", guard =>
{
    guard.SecondInstanceLaunched += newArgs =>
    {
        // Use window.Invoke() to marshal to the UI thread if needed
        Console.WriteLine($"Second instance launched with {newArgs.Length} arg(s)");
    };
});

builder.ConfigureWindow(options =>
{
    options.Title = "My App";
});

var app = builder.Build();
app.Run();

The Blazor extension handles second-instance detection automatically: if another instance is already running, it forwards the current args and exits. The SingleInstanceGuard is registered as a singleton in DI.

Without Blazor ​

csharp
using var guard = HermesApplication.SingleInstance("com.mycompany.myapp");

if (!guard.IsFirstInstance)
{
    guard.NotifyFirstInstance(args);
    return;
}

guard.SecondInstanceLaunched += newArgs =>
{
    Console.WriteLine($"Received args: {string.Join(", ", newArgs)}");
};

var window = new HermesWindow()
    .SetTitle("My App")
    .SetSize(1024, 768);

window.WaitForClose();

Application ID ​

The application ID must contain only alphanumeric characters, hyphens, underscores, and dots. Use a reverse-domain style identifier:

  • com.mycompany.myapp
  • my-app-name

Key Types ​

TypeDescription
SingleInstanceGuardManages the mutex and named pipe listener
IsFirstInstancetrue if this is the primary instance
SecondInstanceLaunchedEvent fired when another instance sends args
NotifyFirstInstance(args)Sends args to the primary instance

Coming Soon ​

  • Bring-to-front behavior on second launch
  • Integration with window focus