Showing posts with label single instance application in c#. Show all posts
Showing posts with label single instance application in c#. Show all posts

Thursday, October 16, 2008

Allowing Only One Instance of an Application in C#.Net

Hi Readers,

Standard Windows Form application can be launched multiple times and each instance used completely independently of the others। However, sometimes it is important to restrict the user in launching multiple instance of a program।

See the code snippet below in how to restrict lunching of multiple instance of an application.

Required Namespace:
System.Diagnostics

C# Code:
static void Main()
{
// Detect existing instances
string processName = Process।GetCurrentProcess().ProcessName;
Process[] instances = Process.GetProcessesByName(processName);
if (instances.Length > 1)
{
MessageBox.Show(”This application is already running.”);
return;
}
// End of detection
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}

Hope this would help you folks. Thanks.

Best Regards,
Shaper Jabneel