0

First, sorry about question title, if there are better suggestions, I'm opened to them.

What I do is I have a program which launch an executable. I have no control over the executable. Executable may crash (then option to send reports, and everything).

What I'd need to do, is if it crashes, the one which launches it would need to notice, and shut the executable.

Is there a way to have this control?

Cher
  • 2,789
  • 10
  • 37
  • 64
  • Are you using `Process.Start` to launch the process? Does the error come back on standard error? Something like this perhaps: http://stackoverflow.com/a/2709234/328193 – David Sep 15 '16 at 17:15
  • What is this executable, is it a console application? does it close itself when it fails? does it returns any exit code? – sallushan Sep 15 '16 at 17:16
  • If it "crashes", isn't it already "shut"? – HardCode Sep 15 '16 at 17:19
  • You may want to consider using Windows Task Scheduler instead of a separate program, if all you need to do is try to run the program on a schedule. Task Scheduler has options to retry, shut down, or notify when a scheduled process fails. https://www.google.com/search?q=c-sharp+create+windows+task+scheduler&ie=&oe= – Shannon Holsinger Sep 15 '16 at 17:21

1 Answers1

4

If you are using Process.Start then you can wait for the process to complete using Process.WaitForExit() like so:

var proc = Process.Start(...);
proc.WaitForExit();

If you aren't launching the process with Process.Start and this is an existing process then you can use Process.GetProcessByName or Process.GetProcessById first and then wait on it.

It is worth noting though that this won't return until the error report dialog has been dismissed.

Scott Perham
  • 2,410
  • 1
  • 10
  • 20
  • thx!! Will WaitForExit know if it crashes but remain open, with a window from OS indicating that program crashed? – Cher Sep 15 '16 at 17:23
  • No, technically the process is still running while that dialog is open so something monitoring it won't know it's crashed until that Error Report dialog is dismissed – Scott Perham Sep 15 '16 at 17:24
  • FYI - "Error Reporting" is a Windows feature and is enabled by default (unless it's been disabled by a gpo) - it _can_ be disabled via a registry change but typically you won't have much control over the dialog if this is running on a client machine – Scott Perham Sep 15 '16 at 17:27