What I'm trying to do is like
/// <summary>
/// Read zip file into chunks of bytes of largest size possible
/// </summary>
/// <param name="stream"></param>
/// <returns>Enumeration of byte arrays</returns>
private static async Task<IEnumerable<byte[]>> ChunkStreamBytes(FileStream stream)
{
var tasks = new List<Task<byte[]>>();
for (long bytesRemaining = stream.Length; bytesRemaining > 0;)
{
int chunkSize = (int)Math.Min(bytesRemaining, int.MaxValue);
byte[] chunk = new byte[chunkSize];
bytesRemaining -= await stream.ReadAsync(chunk, 0, chunk.Length);
yield return chunk;
}
}
but I get the error
The body of ... cannot be an iterator block because Task> is not an iterator interface type
I thought of trying await Task.WaitAll(...)
where each task is reading a chunk, but I'm not sure if the tasks can run out of order and screw up the file I'm trying to construct. I need to use async
-await
pattern because of the context. Any ideas on the proper solution?