Divide the body of the while into a separate async method:
private async Task WriteAsync(...) { using (var memoryStream = new MemoryStream(...)) { await _pageBlob.WritePagesAsync(memoryStream, ...); } }
Then use the new method:
public void Write() { var tasks = new List<Task>(); while(...) { tasks.Add(WriteAsync(...)); } Task.WaitAll(tasks.ToArray()); }
On the one hand, blocking asynchronous code ( Task.WaitAll ) is usually not a good idea. A more natural approach is to maintain asynchrony:
public async Task WriteAsync() { var tasks = new List<Task>(); while(...) { tasks.Add(WriteAsync(...)); } await Task.WhenAll(tasks); }
source share