Solved

c#.net application + check if already running

Posted on 2016-10-14
5
123 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 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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

697 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