Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where are FIle.ReadAll***Async/WriteAll***Async/AppendAll***Async methods?

There are a bunch of rather convenient methods in File class, like ReadAll***/WriteAll***/AppendAll***.

I'm faced with a number of cases, when I need their asynchronous counterparts, but they just don't exist.

Why? Are there any pitfalls?
I know, that these methods could be easily implemented, but is there any reason to not implement them in the framework out-of-the-box?

like image 719
Dennis Avatar asked Sep 29 '15 06:09

Dennis


1 Answers

"... I need their asynchronous counterparts, but they just don't exist. Why?"

All of the XXXAsync methods that were added to the .Net framework together with async-await (not including new libraries developed with async in mind) are simply wrappers around BeginXXX/EndXXX.

They didn't add any new asynchronous operations, they just converted old ones into new task-based ones. For example this is UdpClient.SendAsync:

public Task<int> SendAsync(byte[] datagram, int bytes)
{
    return Task<int>.Factory.FromAsync(BeginSend, EndSend, datagram, bytes, null);
}

Since there are no File.BeginReadAll and File.EndReadAll it's understandable that there's no File.ReadAllAsync.

Are there any pitfalls?

The only pitfall with implementing these methods is doing so in a truly asynchronous manner and not faking async.

like image 121
i3arnon Avatar answered Nov 02 '22 11:11

i3arnon