【问题标题】:StandardOutput.ReadToEnd Hangs IndefinitelyStandardOutput.ReadToEnd 无限期挂起
【发布时间】: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 上。

标签: c# asp.net process


【解决方案1】:

原来是我的机器的脚本权限问题。一旦我将其更改为 ByPass,它就起作用了。所以,这不是代码!

【讨论】:

    猜你喜欢
    • 2011-11-01
    • 1970-01-01
    • 2017-06-29
    • 2011-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-29
    • 2019-08-01
    相关资源
    最近更新 更多