【问题标题】:how to read an output message from Process [duplicate]如何从流程中读取输出消息[重复]
【发布时间】:2013-12-18 14:57:49
【问题描述】:

我有一个调用 exe 文件的 C# 应用程序。下面是我用来调用进程的代码:

System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.EnableRaisingEvents = false;
proc.StartInfo.FileName = "ContasillabeGame";
proc.StartInfo.Arguments = numeroSillabe + " " + numeroToken;
proc.OutputDataReceived += new DataReceivedEventHandler(OutputToTextArea);
proc.Start();
// Start the asynchronous read of the sort output stream.
proc.WaitForExit();

这里是用于从进程中获取数据的方法。

private void OutputToTextArea(object sendingProcess, DataReceivedEventArgs outLine)
{
    // Collect the sort command output. 
    if (!String.IsNullOrEmpty(outLine.Data))
    {
        System.Console.WriteLine(outLine.Data);
    }
}

使用此代码,我启动了应用程序 ContasillabeGame,但该应用程序没有任何结果。为什么?对于从 ContasillabeGame 发送消息,我使用此代码

发送消息的示例代码:

System.Console.WriteLine("pippo");

【问题讨论】:

  • 您已将 EnableRaisingEvents 设置为 false,然后在从未引发事件时感到惊讶。这应该不足为奇。
  • 我已经编辑了你的标题。请参阅“Should questions include “tags” in their titles?”,其中的共识是“不,他们不应该”。

标签: c# .net xaml process


【解决方案1】:

你应该在开始进程之前将proc.StartInfo.RedirectStandardOutput设置为true,并在调用Start()之后调用proc.BeginOutputReadLine();。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-17
    • 1970-01-01
    • 2019-07-06
    • 1970-01-01
    • 2019-12-12
    • 2017-12-20
    • 2010-10-16
    相关资源
    最近更新 更多