c# - How to monitor another .exe and perform action if it closes -


currently have program opens program. need perform action should program close. pretty new c# use getting me pointed in correct direction.

edit: able current form open external program. form needs close , form needs have function perform action should loader.exe close. have in first form. how code other form know if program has ended.

public static class program {     public static bool openmanager { get; set; }     public static int desknumber { get; set; }      /// main entry point application.      [stathread]     static void main(string[] arguments)     {         /// not move this!         openmanager = false;         myvariable = 0;         /// ----------------          application.enablevisualstyles();         application.setcompatibletextrenderingdefault(false);         application.run(new loader());          if (openmanager)         {             if (myvariable == 1)             {                 system.diagnostics.process startprogram = system.diagnostics.process.start("loader.exe", "-r 52");                 startprogram.enableraisingevents = true;                 application.run(new manager()); 

assuming use process class start other program, can subscribe exited event of process instance:

process myprocess = process.start("myprogram.exe", "arguments"); myprocess.enableraisingevents = true; myprocess.exited += (sender, e) =>      {        // want when process exited.     } 

or, more explicit, declare explicit event handler called when process finishes:

public void onprocessexited(object sender, eventargs e) {      // want when process exited. }  public void startprocess() {     process myprocess = process.start("myprogram.exe", "arguments");     myprocess.enableraisingevents = true;     myprocess.exited += onprocessexited; } 

Comments