C# process hangs on dispose

自古美人都是妖i 提交于 2021-02-09 08:16:36

问题


I am invoking a command line process from C# that does an update from some remote server. Works fine when it can find the remote server and fetch data from it. Also works fine if not connected to a network.

However, when the remote server is unreachable, the external process will try to fetch data indefinitely, and there is no command line options to specify a timeout. So I added a force kill on the process after trying for 15 seconds.

using (var process = new Process {...})
{
    process.Start();
    if (!process.WaitForExit(15 * 1000))
    {
        process.Kill();
    }
}

I can see the process being terminated when process.Kill() is invoked, but then it hangs when exiting the using scope (i.e. when disposing the process).

What's wrong with this? Am I missing something to make sure this process is properly killed?

Thanks!

EDIT:

Process StartInfo is

FileName = command,
Arguments = arguments,
WorkingDirectory = workDir,
CreateNoWindow = true,
UseShellExecute = false

来源:https://stackoverflow.com/questions/50572800/c-sharp-process-hangs-on-dispose

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!