0

Many Windows apps (like Skype or MSN for instance) don't let you start multiple instances, rather trying to run it a 2nd time just leaves the existing version running.

Is this typically done in some simple way - the start-menu shortcut is a 'wrapper' app around the main app - or is there some registry magic you can do to delegate the problem to Windows itself?

Specifically dealing with Win32 here (unmanaged C++) but happy to hear more general solutions as long as they are workable on Windows XP or later.

EDIT: this seems the best duplicate.

Community
  • 1
  • 1
Mr. Boy
  • 60,845
  • 93
  • 320
  • 589
  • This question is already answered multiple times in this site - check http://stackoverflow.com/questions/2285110/restrict-application-to-one-instance-per-shell-session-on-windows for example. – Koran Jun 10 '10 at 10:10
  • 1
    This should give you a good set of options - http://stackoverflow.com/questions/459554/how-do-i-tell-if-one-instance-of-my-program-is-running – Koran Jun 10 '10 at 10:28

3 Answers3

0

As far as i remember, there exist system-wide Mutexes. Set Mutex on first launch, if on launch already set, immediately exit.

Use CreateMutex() call an prepend the name with "Global\" should to the trick.

zerm
  • 2,812
  • 25
  • 17
0
  1. Named Mutex or similar OS-specfic named object. If it exists - app is running.
  2. Lock file somewhere (in temporary directory, etc - create it on program start, remove on program end). Linux software frequently operates this way (some programs store PID in lockfile), but it isn't safe - if you suddenly lose power (electricity cut off), it is possible that lock file won't be deleted.
  3. And you can always enum all running processes and try to find yourself.

There could be more ways to do it, but those are the first ones I could think of.

SigTerm
  • 26,089
  • 6
  • 66
  • 115
0

I just check to see if the process is already running: if it's not start the application, if it's already running bring the window to foreground. The check is done in the Main method.

I get the process name with System.Diagnostics.Process.GetCurrentProcess().ProcessName and check if it's already running System.Diagnostics.Process.GetProcessesByName(). If there are more than 1 processes focus the first of them and then exit.

Adrian Fâciu
  • 12,414
  • 3
  • 53
  • 68