0

To check if an application is running and continue or return back to your previous condition this should make your life easier in order not to exit from any of your current processes

 protected override void OnFormClosing(FormClosingEventArgs e) { Process[] pname = Process.GetProcessesByName("ConsoleApplication"); if (pname.Length == 0) { } else { MessageBox.Show("You cannot exit until the process has finished"); e.Cancel = true; } } 
4
  • FormClosingEventArgs has an option to cancel the form closing event. msdn.microsoft.com/en-us/library/… Commented Apr 5, 2015 at 16:05
  • Well, what have you tried to do that didn't work so far? Commented Apr 5, 2015 at 16:06
  • So you problem is: How to get a list of processes running in my PC and stop the application from exit if there is a process running with a specific name? Process.GetProcessesByName Commented Apr 5, 2015 at 16:08
  • The duplicate is correct, but the answer below from @BrokenGlass is even better Commented Apr 5, 2015 at 16:22

1 Answer 1

1

You can check for an active process with that name:

using System.Diagnostics; //... bool isRunning = Process.GetProcessesByName("blocker.exe").Any(); 

But note that you cannot force your executable to be still running - this will only work if the does not force a shut down of your process ("kill"), but properly asks it to shutdown itself ("close").

Sign up to request clarification or add additional context in comments.

1 Comment

@newbie your original question (now commented out) contains the logic to handle your new question. What's wrong with if and e.Cancel = true?