Starting only one instance of an application

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
 
C

Chris Tacke, eMVP

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
 
A

Alex Chan

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
 
P

Paul G. Tobey [eMVP]

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top