1

I have some code like this:

process.CloseMainWindow();
if (!process.WaitForExit(5000)) { process.Kill(); }

The idea is to let the process exit gracefully, but if it takes longer than 5 seconds, I assume it needs to be killed.

This appears to work in most cases, but if the process has thrown up a windows error message, it just hangs. I previously had only the "process.Kill()" and that worked, so I have to assume it's actually getting stuck on the WaitForExit(5000) call, even though I'm giving it a timeout value.

Any reason this would happen?

EDIT: I'm wondering if maybe it's the CloseMainWindow() that's actually hanging, because of that error message. In which case, would checking the process's "responding" property before trying the CloseMainWindow() method actually return "false"? If so, I could check that and then use Kill if that's the case.

Starfleet Security
  • 1,813
  • 3
  • 25
  • 31
  • Windows is killing the process so an exit never occurs. – jdweng Apr 14 '16 at 19:09
  • No, the process is still running, I can see the window behind the error box. – Starfleet Security Apr 14 '16 at 19:11
  • What about testing if you got a window error with Marshal.GetLastWin32Error. Only kill process if process is running. Test to see if process still exists before killing. – jdweng Apr 14 '16 at 19:13
  • When you press ok on error box does the process terminate? – jdweng Apr 14 '16 at 19:14
  • If I remember correctly, the error box has a "quit" button, which is what I've always clicked on manually (if I happened to run across it happening), so then of course it does end the process. I don't remember what the other button is or what would happen. – Starfleet Security Apr 14 '16 at 19:28
  • 1
    I tried reproducing this with a small WinForms app that starts a console app that just throws a divide by 0 error. It seemed to work as expected. If I closed the Windows error message before the timeout, WaitForExit returned true and process.Kill wasn't executed. If I left the process running it would kill the process, however the Windows error message stayed open. Might be easier to just add a loop and use Process.GetProcessesByName to see if the process is still running. Keep track of how long your loop has been running and call Kill and exit your loop if you exceed the time you want to wait – Marc Apr 14 '16 at 19:53

1 Answers1

0

First, I want to thank Marc for going to the trouble of doing that, it never even occurred to me I could reproduce the error in that way.

In any case, after reading the help file for CloseMainWindow() more carefully, I saw this:

Return Value
Type: System.Boolean
true if the close message was successfully sent; false if the associated 
process does not have a main window or if the main window is disabled (for 
example if a modal dialog is being shown).

So right there it says that a modal dialog (such as the error I was seeing), will cause a problem with this. Now I'm not sure why it didn't just fall through and then get killed anyway after the timeout, but I think I can use this return value to just kill the process in this scenario.

Starfleet Security
  • 1,813
  • 3
  • 25
  • 31
  • You can also check the [HasExited property](https://msdn.microsoft.com/en-us/library/system.diagnostics.process.hasexited(v=vs.110).aspx), doing a `process.Refresh()` before checking it in a while loop. – Quantic Apr 15 '16 at 18:23