Sunday, 30 December 2012 13:54

how to prevent running multiple application instances same time

Written by 
Rate this item
(0 votes)

if you want to prevent running multiple instance
of the same c# application then check the
following lines.It's sometime necessary to prevent
running multiple instances of the same application.

following c# codes shows how to do that -

in your main form under Main() method add the
following method-

[STAThread]
static void Main()
{
checkApplicationInstance();
}

then add the method -

static void checkApplicationInstance()
{
string currPrsName = Process.GetCurrentProcess().ProcessName;
Process[] allProcessWithThisName =
Process.GetProcessesByName(currPrsName);
if (allProcessWithThisName.Length > 1)
{
MessageBox.Show("your application is already
running\nor might be just restarting.\ntry again.",
"your app name", MessageBoxButtons.OK, MessageBoxIcon.Information);
Application.Exit();
}
else
{
Application.Run(new mainForm());

}
}

Read 2909 times
Super User

Email This email address is being protected from spambots. You need JavaScript enabled to view it.

Latest discussions

  • No posts to display.