处理OutputDataReceived等待触发事件完成

时间:2016-03-24 06:56:21

标签: c# console console-application outputstream

我如何等待从OutputDataReceived内部触发的事件?现在它在主要事件和事件之间来回跳跃。这是我想要实现的行为类型:

static void Main(string[] args)
{
    // Create Process
    var process = new Process();
    process.StartInfo.UseShellExecute = false;
    process.StartInfo.RedirectStandardOutput = true;
    process.StartInfo.RedirectStandardInput = true;
    process.StartInfo.FileName = "somefile.exe";
    process.OutputDataReceived += OutputDataReceived;
    process.Start();
    process.BeginOutputReadLine();

    process.StandardInput.WriteLine("CommandToExecuteEventA");
    // Wait untill FireEventA is finished 
    // More code
    process.StandardInput.WriteLine("CommandToExecuteEventB");
    // Wait untill FireEventB is finished
    // More code
    process.StandardInput.WriteLine("CommandToExecuteEventC");
    // Wait untill FireEventC is finished
    // More Code
}

static void OutputDataReceived(object sender, DataReceivedEventArgs e)
{
    if (e.Data.StartsWith("A"))
        FireEventA(this, e);

    if (e.Data.StartsWith("B"))
        FireEventB(this, e);

    if (e.Data.StartsWith("C"))
        FireEventC(this, e);
}

0 个答案:

没有答案