Shaper Jabneel’s Weblog

General Information

Allowing Only One Instance of an Application in .Net

Posted by shaperjabneel on May 13, 2008

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

Posted in .Net Technology | Tagged: | Leave a Comment »

Shaper Jabneel’s Blog

Posted by shaperjabneel on April 12, 2008

Directory

Technologies

Posted in Health | Leave a Comment »

 
Follow

Get every new post delivered to your Inbox.