Singleton Application

Posted by: Rob Diaz-Marino

Date: Feb 3, 2010

Download: SingletonAppSample.zip

Purpose: To ensure only one instance of your application can run at a time, and bring up the currently running instance.

How it Works

  • In the Program.cs file which contains the static Main function, a Mutex object is used to ensure a second instance isn't run. If the Mutex is detected to be in use, the program sends a special custom signal to all open windows and closes.
  • Most windows will simply ignore this message, however in our form we overload the WndProc method to handle this custom message type. When the custom signal is detected, the existing form pops itself up (and can take any other action that you like). Other messages are simply passed along to the base WndProc method.