Starting only one instance of an application

  • Thread starter Thread starter Alex Chan
  • Start date Start date
A

Alex Chan

Dear All,

Are there any method to make sure my application should only be started
once?

As .NET CF application take a considerable time to load, sometimes users
is confused and press more than once of the application icon for invoking.
It will eventually further slower the startup time and multiple instances
are grabbing resources for instantiation

Regards,

Alex
 
If it's a Pocket PC, the platform will enforce it for you. If it's vanilla
CE, you'll need to use a Mutex to ensure it's a singleton.

-Chris
 
Thx Chris,

Yes i am using WinCE. But i only know that a mutex allow exclusively
locking in a assembly/application. Not sure it works for different
instances, does it?

Regards,

Alex
 
There are a lot of more-detailed messages in the Windows CE newsgroups on
this topic, but the basic idea is to create the mutex at the start of your
program. Since you're creating a *named* mutex, it will be globally unique.
If the call succeeds, you check the value of GetLastError() to see if a new
mutex was created (you continue to execute), or if a handle to a mutex
created by another instance of your application was returned (you close the
handle and exit the application, maintaining only a single running
instance). The documentation of CreateMutex() describes the GetLastError()
checking. You're basically using the mutex as a global 'flag' that allows
you to see if anyone else has the flag right now.

Paul T.
 
Back
Top