C# 是否有.NET 4.0取代StreamReader.ReadLineAsync?

C# 是否有.NET 4.0取代StreamReader.ReadLineAsync?,c#,.net,async-await,C#,.net,Async Await,我在一个项目中被.NET4.0卡住了。StreamReader不提供异步或开始/结束版本的ReadLine。底层的Stream对象有BeginRead/BeginEnd,但它们需要一个字节数组,因此我必须实现逐行读取的逻辑 4.0框架中是否有实现这一点的方法?您可以使用任务并行库(Task Parallel Library,TPL)来执行一些尝试执行的异步行为 将同步方法包装到任务中: var asyncTask = Task.Run(() => YourMethod(args, ...)

我在一个项目中被.NET4.0卡住了。StreamReader不提供异步或开始/结束版本的ReadLine。底层的Stream对象有BeginRead/BeginEnd,但它们需要一个字节数组,因此我必须实现逐行读取的逻辑


4.0框架中是否有实现这一点的方法?

您可以使用任务并行库(Task Parallel Library,TPL)来执行一些尝试执行的异步行为

将同步方法包装到任务中:

var asyncTask = Task.Run(() => YourMethod(args, ...));
var asyncTask.Wait(); // You can also Task.WaitAll or other methods if you have several of these that you want to run in parallel.
var result = asyncTask.Result;

如果您需要为StreamReader做很多这方面的工作,那么如果您想要模拟常规异步方法,您可以继续将其作为StreamReader的扩展方法。只需注意使用TPL时的错误处理和其他怪癖。

您可以使用
任务。您没有指定代码的其他部分,因此我不知道您想做什么。我建议您避免使用
Task.Wait
,因为这会阻塞UI线程并等待任务完成,这并不是真正的异步!如果要在任务中读取文件后执行其他操作,可以使用
task.ContinueWith

这里是一个完整的示例,如何在不阻塞UI线程的情况下执行此操作

    static void Main(string[] args)
    {
        string filePath = @"FILE PATH";
        Task<string[]> task = Task.Run<string[]>(() => ReadFile(filePath));

        bool stopWhile = false;

        //if you want to not block the UI with Task.Wait() for the result
        // and you want to perform some other operations with the already read file
        Task continueTask = task.ContinueWith((x) => {
            string[] result = x.Result; //result of readed file

            foreach(var a in result)
            {
                Console.WriteLine(a);
            }

            stopWhile = true;
            });

        //here do other actions not related with the result of the file content
        while(!stopWhile)
        {
            Console.WriteLine("TEST");
        }

    }

    public static string[] ReadFile(string filePath)
    {
        List<String> lines = new List<String>();
        string line = "";
        using (StreamReader sr = new StreamReader(filePath))
        {
            while ((line = sr.ReadLine()) != null)
                lines.Add(line);
        }
        Console.WriteLine("File Readed");
        return lines.ToArray();
    }
static void Main(字符串[]args)
{
字符串filePath=@“文件路径”;
Task Task=Task.Run(()=>ReadFile(filePath));
bool stopWhile=false;
//如果不想用Task.Wait()阻塞UI,请等待结果
//您想对已经读取的文件执行一些其他操作
Task continueTask=Task.ContinueWith((x)=>{
string[]result=x.result;//读取文件的结果
foreach(结果中的var a)
{
控制台写入线(a);
}
stopWhile=true;
});
//此处不执行与文件内容结果无关的其他操作
while(!stopWhile)
{
控制台写入线(“测试”);
}
}
公共静态字符串[]读取文件(字符串文件路径)
{
列表行=新列表();
字符串行=”;
使用(StreamReader sr=新的StreamReader(文件路径))
{
而((line=sr.ReadLine())!=null)
行。添加(行);
}
Console.WriteLine(“文件读取”);
返回行。ToArray();
}

您可以在后台线程中读取。NET4仍然有TPL、线程和后台工作人员。您的问题还不清楚
readlinesync()
主要用于具有其他相关功能的情况,尤其是
async
/
wait
。您是在使用.NET 4.0的异步CTP还是异步目标包?如果没有,您打算如何使用像
readlinesync()
这样的方法?请注意,如果.NET本身已经有了类似于
readlinesync()
的东西,他们可能不会在4.5中添加它。您或其他人必须编写,或者必须已经编写。如果您只需要一些
readlinesync()
的实现,您只需复制即可。毕竟它是开源的。@PeterDuniho不,它不是开源的。单击链接顶部的“许可证”。MS可能会在其他地方通过开放源代码许可证提供完全相同的代码,但不是在那里。@PeterDuniho F#Async。当然可以,但我不想阻止线程池线程。我假设StreamReader.ReadLineAsync()可以在不阻塞线程的情况下完成此操作。