Solved

c#.net application + check if already running

Posted on 2016-10-14
5
48 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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 78

Expert Comment

by:David Johnson, CD, MVP
Comment Utility
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This video discusses moving either the default database or any database to a new volume.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now