views:

415

answers:

3

How can I avoid that a user starts the same program twice? The current implementation tries to do that using "FindWindow", but since it takes some time before the program opens the first window, users regulary manage to start the program twice, causing errors etc.

A: 

You need to use something called Mutex. You can use this to see if an application is running. I believe that Mutext can be used in the compact framework. A good explaination can be found here

Rippo
Unfortunately, .net compact framework only supports local mutexes (which can be used to synchronize threads within a process).Using Process.GetCurrentProcess().ProcessName also doesn't work for me, since the application must run with .net cf 1.
ammoQ
+2  A: 

You have to use a named mutex so it can be used across processes. For whatever (stupid) reason, the CF designers figured CF developers would never need such a thing, so you have 2 options:

  1. P/Invoke CreateMutex and the associated clean up stuff
  2. Use an already written implementation like the SDF's NamedMutex class (which simply does #1 for you) from OpenNETCF.

There is actually a 3rd option as well. The SDF's Application2 class has a couple Run method overloads that wraps this logic for you and enforces app singleton behavior.

ctacke
Found a good description including samples here:http://www.pinvoke.net/default.aspx/kernel32/CreateMutex.html
ammoQ
A: 

Use this: http://msdn.microsoft.com/en-us/netframework/bb943002.aspx

Many of the alternatives out there are either too complicated or don't work all of the time.

Joshua Barker
Thanks a lot, but it's too late for me... not working with .net anymore.
ammoQ

related questions