Process.WaitForExit() triggers too fast

前端 未结 1 1517
既然无缘
既然无缘 2021-01-26 16:27

Here is the code I use to run extern executable (unmanaged) from c# code:

static void Solve()
            {
                Process newProc = new Process();
             


        
相关标签:
1条回答
  • 2021-01-26 16:57

    Your process is probably failing in an unforeseen way. You can only know to read the Output and Error stream and store it in a file (or write it to the console, or eventlog)

    Remember that if you need to read the Error AND the Output streams simultanuously to do it async/eventdriven. Otherwise the streams will block and not produce any output or not the output you're after.

    StreamWriter errorReporter = new StreamWriter("SOLVER-OUTPUT-ERROR.txt", true);
    
    newproc.StartInfo.RedirectStandardOutput = true;
    newproc.StartInfo.RedirectStandardError = true;
    
    newproc.OutputDataReceived += (sender, args) => errorReporter.WriteLine(args.Data);
    newproc.ErrorDataReceived += (sender, args) => errorReporter.WriteLine(args.Data);
    newproc.StartInfo.UseShellExecute=false;
    
    newProc.Start();
    newProc.BeginOutputReadLine();
    newProc.BeginErrorReadLine();
    
    newProc.WaitForExit();
    
    errorReporter.Close();
    
    0 讨论(0)
提交回复
热议问题