Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

c#.net application + check if already running

Posted on 2016-10-14
5
Medium Priority
?
299 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 23

Assisted Solution

by:Snarf0001
Snarf0001 earned 1000 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 1000 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 83

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

618 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