首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >ProcessStartInfo挂在"WaitForExit“上?为什么?

ProcessStartInfo挂在"WaitForExit“上?为什么?
EN

Stack Overflow用户
提问于 2008-09-26 13:46:56
回答 17查看 136.7K关注 0票数 210

我有以下代码:

代码语言:javascript
运行
复制
info = new System.Diagnostics.ProcessStartInfo("TheProgram.exe", String.Join(" ", args));
info.CreateNoWindow = true;
info.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
info.RedirectStandardOutput = true;
info.UseShellExecute = false;
System.Diagnostics.Process p = System.Diagnostics.Process.Start(info);
p.WaitForExit();
Console.WriteLine(p.StandardOutput.ReadToEnd()); //need the StandardOutput contents

我知道我开始的进程的输出大约是7MB长。在Windows控制台中运行它可以正常工作。不幸的是,在编程上,这在WaitForExit上无限期地挂起。另请注意,对于较小的输出(如3KB),此代码不会挂起。

有没有可能ProcessStartInfo内部的StandardOutput不能缓冲7MB?如果是这样,我应该怎么做呢?如果不是,我做错了什么?

EN

Stack Overflow用户

发布于 2018-11-28 01:06:10

我认为使用异步,可能会有一个更优雅的解决方案,即使同时使用standardOutput和standardError也不会出现死锁:

代码语言:javascript
运行
复制
using (Process process = new Process())
{
    process.StartInfo.FileName = filename;
    process.StartInfo.Arguments = arguments;
    process.StartInfo.UseShellExecute = false;
    process.StartInfo.RedirectStandardOutput = true;
    process.StartInfo.RedirectStandardError = true;

    process.Start();

    var tStandardOutput = process.StandardOutput.ReadToEndAsync();
    var tStandardError = process.StandardError.ReadToEndAsync();

    if (process.WaitForExit(timeout))
    {
        string output = await tStandardOutput;
        string errors = await tStandardError;

        // Process completed. Check process.ExitCode here.
    }
    else
    {
        // Timed out.
    }
}

它是基于Mark Byer回答。如果不使用异步方法,则可以使用string output = tStandardOutput.result;而不是await

票数 0
EN
查看全部 17 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/139593

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档