I have a function in my program that detects if the user is already running an instance of my app. If this is the case, I want the currently starting app to shut down.

I've tried a couple of ways of doing this, but I guess this would be the "correct" one

if (IsRunning())
                MsgBox.Show("Don't be gready! One instance is enough! ;)");

However, if I try doing this (first part of my constructor) Windows is telling me the app has stopped working (Windows is checking for a solution to the problem) and I get an error event of 1001 (System.InvalidOperationException) in the event log. IsRunning() is working fine, but it seems the app doesn't like the abrupt end.

Is there any better way of doing this?


Did you already tried this in the form Load event handler?

Ok, I feel like a moron now. I've tried Application.Exit, Form.Close etc etc with no luck. Now I tried moving the whole function into Program.cs and used !IsRunning() -> start, else -> do nothing and that worked. It was only my attempt at closing the partially opened app that failed.


You're certainly no moron!
Think of it : you solved it for yourself, perhaps with the hints of adatapost and me, but you did most of it yourself. You should be proud!

Thanks :) Getting there, I guess, but it's still quite a way for a programmer turned system administrator turned developer ;)