Solved

c#.net application + check if already running

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

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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

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 article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

707 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