为什么即使在进程终止后异步读取仍未完成? [英] Why has an asynchronous Read not completed even after the process is terminated?

查看:41
本文介绍了为什么即使在进程终止后异步读取仍未完成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个进程,它从作为参数给出的文件中读取数据.我已经异步读取 StandardOutput 和同步读取 StandardError.

I have written a Process which reads data from the file given as an argument. I have read the StandardOutput asynchronously and StandardError synchronously.

public static string ProcessScript(string command, string arguments)
{
        Process proc = new Process();
        proc.StartInfo.UseShellExecute = false;
        proc.StartInfo.RedirectStandardOutput = true;
        proc.StartInfo.RedirectStandardError = true;
        proc.StartInfo.FileName = command;
        proc.StartInfo.Arguments = arguments;
        proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; 
        proc.Start();
        string error = null;
        string output = null;
        proc.OutputDataReceived += (sender, outputLine) => 
        { 
            if (outputLine.Data != null) 
            {
                output += outputLine.Data;
            }
        };
        proc.BeginOutputReadLine();
        error = proc.StandardError.ReadToEnd();
        proc.WaitForExit();
        proc.Close();

        //I have not got entire Output
        return output;
} 

过程完成后,我得到输出.但不完全.我只得到部分数据.即使在进程完成其任务后,异步读取也没有结束,所以只有我得到了部分数据.我需要给出的完整字符串.

After the process has been finished I am getting output. But not entirely. I am getting only partial data. The asynchronous reading is not over even after the process completes its task, So only I am getting partial data. I need complete string which is given.

我正在使用 .Net 3.5.我不能使用 ReadToEndAsync 方法

I am using .Net 3.5. I can't use ReadToEndAsync Method

有什么想法吗?

推荐答案

与其处理事件和处理由此产生的问题,不如直接从实际的输出流中读取(假设您使用的是 .NET 4.5,这要归功于它添加的异步功能).

Rather than handling the event, and dealing with the problems that arise with it, you can simply read from the actual output stream directly (assuming you're using .NET 4.5, thanks to its added asynchronous functionality).

public static string ProcessScript(string command, string arguments)
{
    Process proc = new Process();
    proc.StartInfo.UseShellExecute = false;
    proc.StartInfo.RedirectStandardOutput = true;
    proc.StartInfo.RedirectStandardError = true;
    proc.StartInfo.FileName = command;
    proc.StartInfo.Arguments = arguments;
    proc.Start();

    var output = proc.StandardOutput.ReadToEndAsync();
    var error = proc.StandardError.ReadToEndAsync();
    proc.WaitForExit();
    proc.Close();
    var errorContent = error.Result;
    return output.Result;
}

这里由 ReadToEndAsync 表示的 Task 不会真正完成,直到它拥有其结果所表示的全部数据.这意味着您正在等待直到您拥有所有数据,而不是等待过程完成,因为这两者可能不会完全同时发生.

Here the Task represented by ReadToEndAsync won't actually complete until it has the entirety of the data its result represents. This means that you're waiting until you have all of the data rather than waiting for the process to finish, as those two may not be at exactly the same time.

这篇关于为什么即使在进程终止后异步读取仍未完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆