zoya
zoya

Reputation: 483

How to check already running exe in c#.net?

What is the procedure to know that when Everytime the user starts the application it will check if an instance is already running. If its not running it will launch it otherwise it will focus to the current one.i have already tried the code for singleton application but its giving lots of errors. its not running properly. can u provide me the othr alternative solution for this??

Upvotes: 3

Views: 5885

Answers (4)

Ramel
Ramel

Reputation: 1

Using a mutex is not applicable if you're using a background worker. If the backgroundWorker is busy, it can have multiple instances.

Upvotes: 0

Rubens Farias
Rubens Farias

Reputation: 57946

You should use Mutex class, like explained here: Best way to implement singleton in a console application C#?

EDIT: I have this in my code:

class Program
{
    public const string AppName = "...";
    private static readonly Mutex mutex = new Mutex(true, Program.AppName);

    public Program()
    {
        if (!mutex.WaitOne(TimeSpan.Zero, true))
            throw new ApplicationException("Another instance already running");
    }
}

Upvotes: 4

Taylor Leese
Taylor Leese

Reputation: 52310

You could used a named Mutex.

Named system mutexes are visible throughout the operating system, and can be used to synchronize the activities of processes. You can create a Mutex object that represents a named system mutex by using a constructor that accepts a name.

Upvotes: 1

Related Questions