Singleton Application – Single instance application

I am requested to create a singleton “service”. On top of that, it should work and run properly without installation. I know, I know… it is not really a service; in fact it is just a formless single instance application.

Again, there are many ways of implementation. One of the typical ways is to look into every process that is running. But I feel that it should have a better way. Luckily got this solution somewhere on internet. It is using Mutex.

static Mutex mutex = new Mutex(true, "{8F6F0AC4-B9A1-45fd-A8CF-72F04E6BDE8F}");
[STAThread]
static void Main() {
    if(mutex.WaitOne(TimeSpan.Zero, true)) {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Form1 f = new Form1();
        Application.Run();
        mutex.ReleaseMutex();
    }
}

Comments are closed.