C# Process.exited doesnt get called until after window is closed
Asked Answered
M

5

6

So I am writing an application that monitors a console application that was written by another developer. The console application is prone to crashing and because it runs all night i need my application to restart it.

Unfortunaltey when the console app crashes i get the windows message that says "blah has stopped working" And a button that says close program.

I am using System.Diagnostic.Process to start the console application but I cannot determine if the console application has crashed until AFTER the close program button is hit. Process.Responding is always true (there is no windows handle) Process.exited isnt fired till after the close program button is hit.

Any ideas would be greatly helpfull.

Thanks

Mathewson answered 6/3, 2012 at 20:12 Comment(3)
Don't know what you could do about it in C#, but one possible option is to use an automation tool (like AutoIt) to click the Close button for you.Cavan
Are both applications written in .NET?Sherwoodsherwynd
no the console app is not written with .netMathewson
L
2

Disable the windows crash reporting feature. It gets notified by the kernel when a process fails. The process is alive until the crash reporting thingy is done with it.

The feature can be disabled per-process using some API. Disabling Windows error reporting (Dr. Watson) for my process Don't globally disable it to fix a local problem.

Lanam answered 6/3, 2012 at 20:16 Comment(3)
i disabled so it doesnt search for a solution but i still get a window pop up.Mathewson
Implementing a system-wide change just for one application seems a touch heavy-handed.Writhe
I was not clear in the original answer: The feature can be disabled per-process using some API. #1134843Lanam
C
1

Look at ProcessStartInfo.ErrorDialog = false;

Convict answered 30/8, 2012 at 18:50 Comment(1)
This suggestion does not solve the problemTortuosity
D
0
  1. this is not a good case for a console app. A windows service or a scheduled task would be a better fit.

  2. your console app is crashing, because you don't have any error handling

  3. as the other person said, your process is being hung by windows error reporting

If you want my advice, going with the little information that I have, I'll assume it has to be the way it is now, so wrap the whole process in a try/catch and deal with the exception in the catch block so it doesn't bubble up to windows error reporting. Then call the restart from there.

Edit: the person got a sufficient answer, but I'm leaving this up for googlers. I want to note that the wrapper application won't ever get control over the crash of the process without having turned off windows error reporting, so it's an important step in the process.

Dimercaprol answered 6/3, 2012 at 20:19 Comment(0)
W
0

I do not know of any built-in method for handling this.. but you could write a debugger into your watchdog: http://www.codeproject.com/Articles/5275/Writing-a-Debugger-Part-2-The-Debug-Loop

Writhe answered 6/3, 2012 at 20:20 Comment(0)
K
-1

This seems to work for me:

System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startinfo = new System.Diagnostics.ProcessStartInfo();
startinfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startinfo.FileName = @"PATH TO EXE";
startinfo.Arguments = "Arg1 Arg2 Arg3";
startinfo.ErrorDialog = false;
startinfo.RedirectStandardError = true;
startinfo.UseShellExecute = false;
process.StartInfo = startinfo;           
process.Start();
process.WaitForExit();
process.Kill();
Krummhorn answered 25/2, 2013 at 21:31 Comment(1)
I tried this suggestion, and it does not work. Are you sure that this removes the dialog for you?Tortuosity

© 2022 - 2024 — McMap. All rights reserved.