簡體   English   中英

在Windows Server 2008中永遠不會觸發OutputDataReceived事件(在Win7中很好)

[英]OutputDataReceived event is never triggered in Windows Server 2008 (fine in Win7)

我有一個似乎無法診斷的奇怪問題。

我正在調用一個外部二進制文件,等待它完成,然后根據標准輸出傳回結果。 我也想注銷錯誤。

我寫了這個,它在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編譯的。

有什么想法我做錯了嗎?

您可能要檢查代碼訪問安全策略。 您可以使用caspol.exe的-l參數運行caspol.exe ,然后再運行應用程序的-u [ser]。

不要忘記檢查以管理員身份運行應用程序時是否仍然遇到相同的問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM