C# 通过process.Start(startInfo)在另一个进程上的奇怪行为--继续

C# 通过process.Start(startInfo)在另一个进程上的奇怪行为--继续,c#,.net,process,C#,.net,Process,原始位置()不允许正确使用测试后代码。我必须在这里开始一个新问题 < >我们的C(V3.5)需要调用C++可执行文件来处理原始数据文件并生成结果文件。p> 它以前使用过以下代码(没有ReadToEnd()或ReadLine()调用): 现在输入的原始数据文件已更改(比以前更大)。我们对该可执行文件的调用将永远挂起。根据建议,我添加了ReadToEnd()或ReadLine(),但它将挂起这些调用。所以我必须使用startInfo.UseShellExecute=true或设置 startInfo

原始位置()不允许正确使用测试后代码。我必须在这里开始一个新问题

< >我们的C(V3.5)需要调用C++可执行文件来处理原始数据文件并生成结果文件。p> 它以前使用过以下代码(没有ReadToEnd()或ReadLine()调用):

现在输入的原始数据文件已更改(比以前更大)。我们对该可执行文件的调用将永远挂起。根据建议,我添加了ReadToEnd()或ReadLine(),但它将挂起这些调用。所以我必须使用
startInfo.UseShellExecute=true或设置

startInfo.UseShellExecute = false; 
// and with
startInfo.RedirectStandardError = false;
startInfo.RedirectStandardOutput = false;
不知道为什么

以下是我测试的案例:

  • 工作案例:

    ProcessStartInfo startInfo = new ProcessStartInfo();
    startInfo.CreateNoWindow = true;
    startInfo.UseShellExecute = false;
    startInfo.RedirectStandardError = false;
    startInfo.RedirectStandardOutput = false;
    startInfo.WindowStyle = ProcessWindowStyle.Hidden;
    startInfo.FileName = path;
    startInfo.Arguments = rawDataFileName;
    //startInfo.WorkingDirectory = Util.GetParentDirectory(path, 1);
    try
    {
        Process correctionProcess = Process.Start(startInfo);
        correctionProcess.WaitForExit();
    }
    
  • 工作案例:

    ProcessStartInfo startInfo = new ProcessStartInfo();
    startInfo.CreateNoWindow = true;
    startInfo.UseShellExecute = true;
    startInfo.RedirectStandardError = false;//can be commented out
    startInfo.RedirectStandardOutput = false;//can be commented out
    startInfo.WindowStyle = ProcessWindowStyle.Hidden;
    startInfo.FileName = path;
    startInfo.Arguments = rawDataFileName;
    startInfo.WorkingDirectory = Util.GetParentDirectory(path, 1);
    try
    {
        Process correctionProcess = Process.Start(startInfo);
        correctionProcess.WaitForExit();
    }
    
    ProcessStartInfo startInfo = new ProcessStartInfo();
    startInfo.CreateNoWindow = true;
    startInfo.UseShellExecute = false;
    startInfo.RedirectStandardError = true;
    startInfo.RedirectStandardOutput = true;
    startInfo.WindowStyle = ProcessWindowStyle.Hidden;
    startInfo.FileName = path;
    startInfo.Arguments = rawDataFileName;
    //startInfo.WorkingDirectory = Util.GetParentDirectory(path, 1);
    try
    {
        Process correctionProcess = Process.Start(startInfo);
        Log.logItem(LogType.Performance, "read errorMsg @@@@", "DPTM::correctData()", "");
        string errorMsg = correctionProcess.StandardError.ReadToEnd(); // <-- Hangs here!
        Log.logItem(LogType.Performance, "read errorMsg", "DPTM::correctData()", "errorMsg=" + errorMsg);
        string outputMsg = correctionProcess.StandardOutput.ReadToEnd();
        Log.logItem(LogType.Performance, "read outputMsg", "DPTM::correctData()", "outputMsg=" + outputMsg);
        correctionProcess.WaitForExit();
    }
    
  • 不工作情况(使用
    ReadLine()
    ):

  • 不起作用的情况:

    ProcessStartInfo startInfo = new ProcessStartInfo();
    startInfo.CreateNoWindow = true;
    startInfo.UseShellExecute = true;
    startInfo.RedirectStandardError = false;//can be commented out
    startInfo.RedirectStandardOutput = false;//can be commented out
    startInfo.WindowStyle = ProcessWindowStyle.Hidden;
    startInfo.FileName = path;
    startInfo.Arguments = rawDataFileName;
    startInfo.WorkingDirectory = Util.GetParentDirectory(path, 1);
    try
    {
        Process correctionProcess = Process.Start(startInfo);
        correctionProcess.WaitForExit();
    }
    
    ProcessStartInfo startInfo = new ProcessStartInfo();
    startInfo.CreateNoWindow = true;
    startInfo.UseShellExecute = false;
    startInfo.RedirectStandardError = true;
    startInfo.RedirectStandardOutput = true;
    startInfo.WindowStyle = ProcessWindowStyle.Hidden;
    startInfo.FileName = path;
    startInfo.Arguments = rawDataFileName;
    //startInfo.WorkingDirectory = Util.GetParentDirectory(path, 1);
    try
    {
        Process correctionProcess = Process.Start(startInfo);
        Log.logItem(LogType.Performance, "read errorMsg @@@@", "DPTM::correctData()", "");
        string errorMsg = correctionProcess.StandardError.ReadToEnd(); // <-- Hangs here!
        Log.logItem(LogType.Performance, "read errorMsg", "DPTM::correctData()", "errorMsg=" + errorMsg);
        string outputMsg = correctionProcess.StandardOutput.ReadToEnd();
        Log.logItem(LogType.Performance, "read outputMsg", "DPTM::correctData()", "outputMsg=" + outputMsg);
        correctionProcess.WaitForExit();
    }
    
    ProcessStartInfo startInfo=新的ProcessStartInfo();
    startInfo.CreateNoWindow=true;
    startInfo.UseShellExecute=false;
    startInfo.RedirectStandardError=true;
    startInfo.RedirectStandardOutput=true;
    startInfo.WindowStyle=ProcessWindowStyle.Hidden;
    startInfo.FileName=路径;
    Arguments=rawDataFileName;
    //startInfo.WorkingDirectory=Util.GetParentDirectory(路径1);
    尝试
    {
    过程修正过程=过程开始(startInfo);
    Log.logItem(LogType.Performance,“read errorMsg@@@”,“DPTM::correctData(),”);
    
    string errorMsg=correctionProcess.StandardError.ReadToEnd();//您在错误/输出缓冲区上处于死锁状态。您的第二个进程正在等待缓冲区释放,而您正在等待该进程完成。您需要异步读取输出/错误流以防止出现这种情况

    看看你的MSDN样本:哇,这个样本真的让人困惑……如果他们只是把它限制在std out或std error上,那就更容易理解了

    您需要首先设置数据处理程序

    correctionProcess.OutputDataReceived += new DataReceivedEventHandler(OutputDataHandler);
    
    然后打电话

    p.BeginOutputReadLine();
    
    就像comment[confusingly]建议的那样,不要尝试读取那里的输出,而是使用处理程序

        private void OutputDataHandler(object sendingProcess, DataReceivedEventArgs data)
        {
            if (data.Data != null)
            {
                // append data.Data to your internal buffer (StringBuilder?)
            }
        }
    

    下面是Miscrosoft的示例代码。我的案例5与之相同。它在这里:所以…我不确定你在说什么。
        private void OutputDataHandler(object sendingProcess, DataReceivedEventArgs data)
        {
            if (data.Data != null)
            {
                // append data.Data to your internal buffer (StringBuilder?)
            }
        }