【发布时间】:2021-04-23 05:54:04
【问题描述】:
首先,我尝试了几种解决方案,包括此处列出的解决方案。 ProcessStartInfo hanging on "WaitForExit"? Why? 和 StandardOutput.ReadToEnd() hangs
没有一个对我有用,而且都超时或挂起。你在我的代码中看到我做错了什么明显的事情吗?基本上,我正在尝试通过 C# 运行 shell 脚本。当您手动运行脚本时,该脚本运行良好,并且需要一段时间。 (就像一分钟)在我的代码中,它挂在 ReadToEnd 上。这是代码...
public psResult RunScript(string scriptToExecute, string scriptArgument)
{
var logEnabled = true;
scriptToExecute = ConfigurationManager.AppSettings["s-path"] + scriptToExecute;
if (!System.IO.File.Exists(scriptToExecute))
return new psResult
{ HasErrors = true, ErrorMessage = $"{scriptToExecute} is not found!" };
var process = new Process
{
StartInfo =
{
UseShellExecute = false,
RedirectStandardOutput = true,
FileName = ConfigurationManager.AppSettings["exe"],
Arguments = $"{scriptToExecute} {scriptArgument}"
}
};
string logCapture = "";
try
{
process.Start();
logCapture = process.StandardOutput.ReadToEnd();
process.WaitForExit();
}
catch (Exception e)
{
return new psResult
{ HasErrors = true, ErrorMessage = $"{e.InnerException}", RawResult = logCapture };
}
return new psResult
{ HasErrors = false, ErrorMessage = $"{scriptToExecute}.", RawResult = logCapture };
}
【问题讨论】:
-
正在执行的脚本是什么?这会产生任何输出吗?
-
这有时是意料之中的 - 查看异步选项 - stackoverflow.com/questions/12566166/…
-
@Jawad 这是一个 powershell 脚本。做一些远程垃圾并将输出发送到屏幕。
-
@Ctznkane525 我也试过了,它挂在 Peek 上。