在C#中,如果程序不断输出,您如何从DOS程序中读取?

时间:2014-03-03 15:54:02

标签: c# multithreading cmd

我找到了许多关于如何执行cmd.exe并执行命令的编码示例,甚至执行nslookup并进行交互,但我遇到的问题是使用特定的dos程序,当它启动时,它不会停止“输出”。这里有一些代码,我将发表评论以及我从C#中获得的错误

以下是我如何以更高级的方式设置它,以便我可以从事件

接收程序的输出
public void StartApplication(string appNameAndPath)
{
        StreamReader outputStream;
        Process p = new Process();
        p.StartInfo.FileName = appNameAndPath;
        p.StartInfo.RedirectStandardOutput = true;
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.CreateNoWindow = false;//for now just so I can see it
        p.Start();
//here is my advanced example
if(advanced == true)
{
        outputStream = p.StandardOutput;
        DoReadOutPut();
}
else
{//here is a simple example 
    while (p.StandardOutput.ReadLine() != null)  //this hangs here until the application exists
    {
        txt += (p.StandardOutput.ReadLine());
    }
}
}

 void DoReadOutput()
 {
        outputStream.BaseStream.BeginRead( readOutputBuffer, 0, readOutputBuffer.Length, new AsyncCallback( OnReadOutputCompleted ), null );  
       //this does sometimes fire but only with 0 bytes, on other dos programs it would say Memory read not allowed
 }

 void OnReadOutputCompleted( IAsyncResult result )
 {
        int cbRead = outputStream.BaseStream.EndRead( result );
        ProcessOutput( readOutputBuffer, cbRead );
        DoReadOutput();
 }

 private void ProcessOutput(byte[] buffer, int cbRead)
 {
        string text = p.StartInfo.StandardOutputEncoding.GetString(buffer, 0, 10000); //this is where it hangs until the program exits or is not writing anymore
        this.Invoke((Action)delegate
        {

            SetTextBoxValue(text);//im doing this because im on another thread otherwise textBox1.Text - text"

        });
  }

我不想使用API​​和GetText并为ReadLastLine创建引擎,任何人都可以帮助我吗?我想你会想要一个示例exe,创建一个C#应用程序,while(true){Console.WriteLine("bla");}就足够作为示例exe而不是我遇到麻烦的exe。 exe接管dos窗口并具有“旧学校界面”

2 个答案:

答案 0 :(得分:3)

async / await可以在这里帮助....

await Exec(yourExe,parameters);

Task Exec(string exe,string args)
{
    var tcs = new TaskCompletionSource<object>();

    ProcessStartInfo psi = new ProcessStartInfo();
    psi.FileName = exe;
    psi.UseShellExecute = false;
    psi.RedirectStandardOutput = true;
    psi.Arguments = args;

    var proc = Process.Start(psi);

    proc.OutputDataReceived += (s, e) =>
    {
        this.Invoke((Action) (()=>richTextBox1.AppendText(e.Data + Environment.NewLine)));
    };

    proc.Exited += (s, e) => tcs.SetResult(null);

    proc.EnableRaisingEvents = true;
    proc.BeginOutputReadLine();

    return tcs.Task;
}

答案 1 :(得分:1)

您需要处理回调事件以读取流:

  startInfo.UseShellExecute        = false;
  startInfo.CreateNoWindow         = true;
  startInfo.RedirectStandardOutput = true;
  startInfo.RedirectStandardError  = true;

  Process proc = new Process();
  proc.StartInfo           = startInfo;
  proc.ErrorDataReceived  += new DataReceivedEventHandler(DataReceiveHandler);
  proc.OutputDataReceived += new DataReceivedEventHandler(DataReceiveHandler);
  proc.Start();
  proc.BeginErrorReadLine();
  proc.BeginOutputReadLine();
  proc.WaitForExit();