All about multithreading.

Wednesday, August 29, 2007

Single Instance Application

Single instance application can be implemented using Mutex. Mutex is a synchronizing object that can be used across processes. When the first instance of an application is created, we create a named mutex. In the following attempts we check if the named mutex is already created.

Using remoting, we can send command parameters to the first instance.


private static bool IsRunning()
{
     bool bCreatedNew;


     // Parameters to create Mutex object
     // 1. initiallyOwned = true
     // this gets the calling thread initial ownership of the mutex
     // 2. name of the mutex
     // 3. createdNew
     // When this method returns, contains a Boolean that is true

     // if the calling thread was granted initial ownership
     // of the mutex; otherwise, false.
     // This parameter is passed uninitialized.
     //
     _AppMutex = new Mutex(true, _MutexKey, out bCreatedNew);
     if (bCreatedNew)
          _AppMutex.ReleaseMutex();
     return !bCreatedNew;
}


Download this project.

No comments:

Blog Archive

About Me

One of my friends said "if you want to learn something, write a book about it". I wanted to learn all about multithreading and instead of writing a book, I am trying to blog. Thank you for stopping. I appreciate your feedback. Sreekanth