问题
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.
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/867050/net-compact-framework-avoid-program-being-started-twice-concurrently