简体   繁体   English

WaitForExit没有正确超时,只是挂起

[英]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. 我们的想法是让流程优雅地退出,但如果花费的时间超过5秒,我认为它需要被杀死。

This appears to work in most cases, but if the process has thrown up a windows error message, it just hangs. 这似乎在大多数情况下都有效,但如果进程抛出了一条Windows错误消息,它就会挂起。 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. 我以前只有“process.Kill()”并且有效,所以我不得不假设它实际上卡在WaitForExit(5000)调用上,即使我给它一个超时值。

Any reason this would happen? 这会发生什么原因?

EDIT: I'm wondering if maybe it's the CloseMainWindow() that's actually hanging, because of that error message. 编辑:我想知道是否可能是由于该错误消息而实际挂起的CloseMainWindow()。 In which case, would checking the process's "responding" property before trying the CloseMainWindow() method actually return "false"? 在这种情况下,在尝试CloseMainWindow()方法之前检查进程的“响应”属性实际返回“false”? If so, I could check that and then use Kill if that's the case. 如果是这样,我可以检查,然后使用Kill,如果是这样的话。

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. 首先,我要感谢Marc去做那件事的麻烦,我甚至都没想到我会以这种方式重现错误。

In any case, after reading the help file for CloseMainWindow() more carefully, I saw this: 无论如何,在仔细阅读CloseMainWindow()的帮助文件后,我看到了这个:

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. 现在我不确定它为什么不会掉线然后在超时后被杀死但是我想我可以使用这个返回值来杀死这个场景中的进程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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