Loofer
Loofer

Reputation: 6963

In dotnet core how can I ensure only one copy of my application is running?

In the past I have done something like this

private static bool AlreadyRunning()
{
    var processes = Process.GetProcesses();
    var currentProc = Process.GetCurrentProcess();
    logger.Info($"Current proccess: {currentProc.ProcessName}");
    foreach (var process in processes)
    {
        if (currentProc.ProcessName == process.ProcessName && currentProc.Id != process.Id)
        {
            logger.Info($"Another instance of this process is already running: {process.Id}");
            return true;
        }
    }
    return false;
}

Which has worked well. In the new dotnet core world everything has a process name of dotnet so I can only run one dotnet app at a time! Not quite what I want :D

Is there an ideal way of doing this in dotnet? I see mutex suggested but I am not sure I understand the possible downsides or error states running on other systems than a windows machine.

Upvotes: 3

Views: 4069

Answers (2)

Loofer
Loofer

Reputation: 6963

In the end I used a mutex and it seeeeeems okay.

I grabbed the code from here What is a good pattern for using a Global Mutex in C#?

The version of core I am using does not seem to have some of the security settings stuff so I just deleted it. I am sure it will be fine. (new Mutex only takes 3 parameters)

private static void Main(string[] args)
{
    LogManager.Configuration = new XmlLoggingConfiguration("nlog.config");
    logger = LogManager.GetLogger("console");
    logger.Info("Trying to start");

    const string mutexId = @"Global\{{guid-guid-guid-guid-guid}}";

    bool createdNew;
    using (var mutex = new Mutex(false, mutexId, out createdNew))
    {
        var hasHandle = false;
        try
        {
            try
            {
                hasHandle = mutex.WaitOne(5000, false);
                if (!hasHandle)
                {
                    logger.Error("Timeout waiting for exclusive access");
                    throw new TimeoutException("Timeout waiting for exclusive access");
                }
            }
            catch (AbandonedMutexException)
            {
                hasHandle = true;
            }

            // Perform your work here.
            PerformWorkHere();
        }
        finally
        {
            if (hasHandle)
            {
                mutex.ReleaseMutex();
            }
        }
    }
}

Upvotes: 4

Set
Set

Reputation: 49789

.NET Core now supports global named mutex. From PR description, that added that functionality:

  • On systems that support thread process-shared robust recursive mutexes, they will be used
  • On other systems, file locks are used. File locks, unfortunately, don't have a timeout in the blocking wait call, and I didn't find any other sync object with a timed wait with the necessary properties, so polling is done for timed waits.

Also, there is a useful note in Named mutex not supported on Unix issue about mutex name, that should be used:

By default, names have session scope and sessions are more granular on Unix (each terminal gets its own session). Try adding a "Global" prefix to the name minus the quotes.

Upvotes: 9

Related Questions