Solved

c#.net application + check if already running

Posted on 2016-10-14
5
102 Views
Last Modified: 2016-10-14
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
0
Comment
Question by:dkilby
5 Comments
 
LVL 22

Assisted Solution

by:Snarf0001
Snarf0001 earned 250 total points
ID: 41844140
Mutexes are very handy for things like this.  Cross process locking mechanism.

https://msdn.microsoft.com/en-us/library/system.threading.mutex(v=vs.110).aspx

Basically you create it with a specific value, if it comes back as non-owned, then another process is already running in it.

var owned = false;
using (var mutex = new Mutex(true, "<<guid or long string, often based on the fully qualified assembly name>>", out owned))
{
	if(!owned) return;
	else
	{
		//do all your work
	}
}

Open in new window

0
 
LVL 20

Accepted Solution

by:
Russ Suter earned 250 total points
ID: 41844168
More simply than that you could have code in the startup of your application that looks something like this:
Process[] runningProcesses = Process.GetProcessesByName("myApplication");
if (runningProcesses.Length > 0)
{
    // program already running, write code to gracefully exit here
}

Open in new window

https://msdn.microsoft.com/en-us/library/z3w4xdc9%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396
0
 

Author Comment

by:dkilby
ID: 41844173
both these work - thank you - is there code that works best for stopping the instance that just started while leaving the other to still run ?
0
 
LVL 20

Expert Comment

by:Russ Suter
ID: 41844194
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());
        }

Open in new window

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());
            }
        }

Open in new window

0
 
LVL 80

Expert Comment

by:David Johnson, CD, MVP
ID: 41844223
if you are starting this process using task scheduler then simply check the checkbox to task scheduler to only allow 1 copy of the task to run at a time
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question