简体   繁体   中英

WaitForExit not timing out correctly, just hangs

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM