Wednesday, April 7, 2010

Single Instance WPF Application

In this article, I will share a basic simple ways to create a single instance application. Though there are multiple ways to do it, this is what I did in one my recent projects. You may get a stupid answer - "This is simple and fast".

Method 1:

Add the following code to your startup window (which is usually App.xaml.cs) after InitializeComponent().

Process thisProcess = Process.GetCurrentProcess();

if(Process.GetProcessesByName(thisProcess.ProcessName).Length >1 )
{

// If ther is more than one, than it is already running.

MessageBox.Show("Another instance of your app is already running.");

Application.Current.Shutdown();

return;

}


No comments:

Post a Comment

Combinable Enumerations         [Flags]         public enum SomeFlags         {             NoColor = 0,             Red = 1,        ...