Skip to content

C# solutions to check for other application instance on local host or LAN and exits the 2nd instance and uses ILogger for Diagnostic Logging

Notifications You must be signed in to change notification settings

Vinz68/SingleInstanceApplication

Repository files navigation

SingleInstanceApplication

This repository contains 2 different ways of single instance applications in C#.

It is platform independent and can be run on Windows, Linux and MacOS.

The usage is simple in both examples:

    var singleInstanceWrapper = new SingleInstanceAppWrapper()

    // Check if the application is already running
    if (!singleInstanceWrapper.IsApplicationFirstInstance())
    {
        Console.WriteLine("Application is already running.");
        return;
    }

The examples are using the Microsoft.Extensions.Hosting library for the host builder to use ILogger for logging output, but this is not required. You can use the examples without the Microsoft.Extensions.Hosting library by replacing the ILogger with Console.WriteLine.

SingleInstanceApp_using_Mutex

This example project demonstrates how to prevent more than one instance of an application from running at the same time on a computer. It is using a Mutex to create a named system-wide lock.

SingleInstanceApp_using_UDP

This example project demonstrates how to prevent more than one instance of an application from running at the same time on the local host (this computer), or on the entire LAN.

This example uses UDP messages to check if the application is already running. The default listen UDP port is 56253, but you can change it in the options. It uses also UDP port +1 for sending and receiving UDP messages. These ports must be open in the firewall, when running on the LAN.

Most SingleInstanceAppOptions are optional, but you should set the ApplicationGuid to a unique GUID.

About

C# solutions to check for other application instance on local host or LAN and exits the 2nd instance and uses ILogger for Diagnostic Logging

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages