dkilby
asked on
c#.net application + check if already running
I have an application that runs automatically every few mins, sometimes it runs long and a new instance will try to start, how can i check if it is already running and if so just shut down the newest instance that started up leaving the original to still continue
thanks
thanks
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
It depends entirely on where your code execution is. Assuming this either a Windows Forms application or a console application I would expect your project has a file called Program.cs. In it there is typically a Main() function that looks something like this:
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new form1());
}
You can simply wrap that 3rd line in a conditional statement to make it look like this: static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Process[] runningProcesses = Process.GetProcessesByName("myApplication");
if (runningProcesses.Length == 0)
{
Application.Run(new form1());
}
}
ASKER