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
2009-05-15 05:56:21
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
2009-05-15 07:02:17
+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:
- P/Invoke CreateMutex and the associated clean up stuff
- 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
2009-05-15 16:43:51
Found a good description including samples here:http://www.pinvoke.net/default.aspx/kernel32/CreateMutex.html
ammoQ
2009-05-18 05:14:11
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
2010-08-13 18:18:25