0

I want to ensure that only one instance of my WinForm application is running at a time. I use separate utility to run my application.

In that utility, I want to ensure that my app's EXE is not called by my utility if there is already a running instance of my app. To check if there is a already running instance or not, I have used Process.GetProcessesByName (in my utility) which is pretty much suggested by many people. It works in normal circumstances.

However, we found that if user keeps double clicking (too fast) on the utility (which starts an app), he ends up getting more than one instances (number of instances are not equal to number of double clicks). How can we handle this case?

I think, code which checks for the already running process executes by the time first instance is up & running and it's process details are available through Process.GetProcessesByName.

Note: Whether to allow multiple instances or not is dependent on the configuration in my utility.

Learner
  • 4,661
  • 9
  • 56
  • 102
  • You could potentially try and take a `Mutex`, which will fail for other processes and they can exit immediately. It can be difficult to implement this properly .. although Sam Saffron has put together a nice answer here: http://stackoverflow.com/questions/229565/what-is-a-good-pattern-for-using-a-global-mutex-in-c/229567 – Simon Whitehead Feb 06 '15 at 10:14
  • @nvoigt: I have explained my scenario after it was marked as duplicate. In my case, I want to check if my app is running or not from some other utility; not from the app itself. Please remove "duplicate" mark. – Learner Feb 06 '15 at 10:34
  • @CsharpLearner You should still use a mutex instead of `Process.GetProcessesByName` because of the very same scenario you describe. If you have problems with the mutex usage, read the duplicate carefully and ask a new question about the mutex problem you encounter. – nvoigt Feb 06 '15 at 11:13
  • 2
    Alternatively, you could look into not allowing to double-click your program that fast, using, guess what? a mutex :) – nvoigt Feb 06 '15 at 11:15

0 Answers0