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;
            }

        }
  • FormClosingEventArgs has an option to cancel the form closing event. https://msdn.microsoft.com/en-us/library/system.componentmodel.canceleventargs.cancel(v=vs.110).aspx – Martijn van Put Apr 05 '15 at 16:05
  • Well, what have you tried to do that didn't work so far? – Yuval Itzchakov Apr 05 '15 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](https://msdn.microsoft.com/en-us/library/z3w4xdc9(v=vs.110).aspx) – Steve Apr 05 '15 at 16:08
  • The duplicate is correct, but the answer below from [@BrokenGlass](http://stackoverflow.com/users/329769/brokenglass) is even better – Steve Apr 05 '15 at 16:22

1 Answers1

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").

BrokenGlass
  • 158,293
  • 28
  • 286
  • 335
  • @newbie your original question (now commented out) contains the logic to handle your new question. What's wrong with if and e.Cancel = true? – Steve Apr 05 '15 at 16:26