【问题标题】:Reading from StandardError and StandardOutput hangs从 StandardError 和 StandardOutput 读取挂起
【发布时间】:2014-07-12 19:53:32
【问题描述】:

我从这里使用了灵魂: ProcessStartInfo hanging on "WaitForExit"? Why? 创建一个运行进程并获取它的 StandardOutput 和 StandardError。 问题是,它仍然挂起! 我相信这可能是因为我使用的是 cmd。 我的代码中的“noJava”实际上是一个 .class 文件的文件路径,它打印出一个命令并将其发送到统一服务器。 如果我在 cmd 中手动调用java -cp E:/Java/src/ Lift,我首先会收到 1 行代码:Command: objects["Door"].transform.position.x += ((new Vector3(0.0,1.0,0.0))).x*0.6;objects["Door"].transform.position.y += ((new Vector3(0.0,1.0,0.0))).y*0.6;objects["Door"].transform.position.z += ((new Vector3(0.0,1.0,0.0))).z*0.6;,然后,如果我将窗口切换到统一,我会收到其余的消息(我相信服务器只是接受连接,而统一是活动窗口)。 现在,如果我尝试这样做,它会挂起:

using (run_process = new Process())
        {
            run_process.StartInfo.FileName = "cmd";
            run_process.StartInfo.Arguments = " /C java -cp "+noJava;
            //run_process.StartInfo.CreateNoWindow = true;
            run_process.StartInfo.UseShellExecute = false;
            run_process.StartInfo.RedirectStandardOutput = true;
            run_process.StartInfo.RedirectStandardError = true;

        StringBuilder output = new StringBuilder();
        StringBuilder error = new StringBuilder();

        using (AutoResetEvent outputWaitHandle = new AutoResetEvent(false))
            using (AutoResetEvent errorWaitHandle = new AutoResetEvent(false))
        {
            run_process.OutputDataReceived += (sender, e) => {
                if (e.Data == null)
                {
                    outputWaitHandle.Set();
                }
                else
                {
                    output.AppendLine(e.Data);
                }
            };
            run_process.ErrorDataReceived += (sender, e) =>
            {
                if (e.Data == null)
                {
                    errorWaitHandle.Set();
                }
                else
                {
                    error.AppendLine(e.Data);
                }
            };

            run_process.Start();

            run_process.BeginOutputReadLine();
            run_process.BeginErrorReadLine();

            if (run_process.WaitForExit(3000) &&
                    outputWaitHandle.WaitOne(3000) &&
                    errorWaitHandle.WaitOne(3000))
            {
                // Process completed. Check process.ExitCode here.
            }
            else
            {
                // Timed out.
            }
        }
        }

我已经尝试解决此问题 2 天了,但仍然无法使其正常工作。 请帮忙!

【问题讨论】:

  • unity 在非活动窗口时暂停,用内置应用试试
  • @LearnCocos2D 即使有一个构建应用程序,它也不起作用......还有其他想法吗?
  • 对于遇到同样问题的其他人:如果最终找到解决方案。问题是,unity 在进入播放模式时会启动 adb.exe,这会阻止服务器正常关闭。漏洞链接:forum.unity3d.com/threads/…

标签: c# cmd unity3d


【解决方案1】:

对于遇到相同问题的其他人:如果最终找到解决方案。

问题是,unity 在进入播放模式时会启动 adb.exe,导致服务器无法正常关闭。错误链接: http://forum.unity3d.com/threads/bug-unity-runs-adb-exe-when-entering-play-mode-in-the-editor.148176/

写了一个小的“adb.exe”进程杀手后,一切都开始正常了! :D

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多