【问题标题】:OutputDataReceived event is never triggered in Windows Server 2008 (fine in Win7)在 Windows Server 2008 中永远不会触发 OutputDataReceived 事件(在 Win7 中正常)
【发布时间】:2012-09-11 18:03:53
【问题描述】:

我有一个奇怪的问题,我似乎无法诊断。

我正在调用外部二进制文件,等待它完成,然后根据标准输出传回结果。我也想注销错误。

我写了这个,它在 Windows 7 中很有效

namespace MyApp
{
    class MyClass
    {

        public static int TIMEOUT = 600000;
        private StringBuilder netOutput = null;
        private StringBuilder netError = null;

        public ResultClass runProcess()
        {

            using (Process process = new Process())
            {
                process.StartInfo.FileName = ConfigurationManager.AppSettings["ExeLocation"];
                process.StartInfo.WorkingDirectory = Path.GetDirectoryName(ConfigurationManager.AppSettings["ExeLocation"]);

                process.StartInfo.UseShellExecute = false;

                process.StartInfo.RedirectStandardOutput = true;
                process.OutputDataReceived += new DataReceivedEventHandler(NetOutputDataHandler);
                netOutput = new StringBuilder();

                process.StartInfo.RedirectStandardError = true;
                process.ErrorDataReceived += new DataReceivedEventHandler(NetErrorDataHandler);
                netError = new StringBuilder();

                process.Start();

                process.BeginOutputReadLine();
                process.BeginErrorReadLine();


                if (process.WaitForExit(TIMEOUT))
                {
                    // Process completed handle result
                    //return my ResultClass object
                }
                else
                {
                    //timed out throw exception
                }
            }


        }

        private void NetOutputDataHandler(object sendingProcess,
              DataReceivedEventArgs outLine)
        {
            //this is being called in Windows 7 but never in Windows Server 2008
            if (!String.IsNullOrEmpty(outLine.Data))
            {
                netOutput.Append(outLine.Data);
            }
        }

        private void NetErrorDataHandler(object sendingProcess,
            DataReceivedEventArgs outLine)
        {
            //this is being called in Windows 7 but never in Windows Server 2008
            if (!String.IsNullOrEmpty(outLine.Data))
            {
                netError.Append(outLine.Data);
            }
        }


    }
}

所以我将它安装在 Windows Server 2008 机器上,并且从未调用过 NetOutputDataHandler 和 NetErrorDataHandler 处理程序。

该应用是为 .NET v4 编译的。

任何想法我做错了什么?

【问题讨论】:

    标签: c# .net process system.diagnostics


    【解决方案1】:

    您可能需要检查代码访问安全策略。 您可以首先运行 caspol.exe 并使用 caspol.exe 的 -l 参数来运行应用程序的 -u[ser]。

    别忘了检查以管理员身份运行应用时是否仍然遇到同样的问题

    【讨论】:

    • 谢谢,我以管理员身份运行,出现同样的问题。奇怪的是,如果我使用非异步标准输出(例如 ReadToEnd()),问题不会发生,但我无法以这种方式捕获 Std 和 Err 流
    猜你喜欢
    • 2020-06-06
    • 1970-01-01
    • 1970-01-01
    • 2018-02-20
    • 2011-11-19
    • 2019-02-20
    • 1970-01-01
    • 1970-01-01
    • 2011-10-03
    相关资源
    最近更新 更多