I have the following method:
public void Write() {
var tasks = new List<Task>();
while(...) {
var memoryStream = new MemoryStream(...);
var task = _pageBlob.WritePagesAsync(memoryStream, ... );
tasks.Add(task);
}
Task.WaitAll(tasks.ToArray());
}
How to correctly dispose memoryStream
that in Task
? I need to dispose the memoryStream
object when the task is finished.
MemoryStream does not have any unmanaged resources to dispose, so you don't technically have to dispose of it. The effect of not disposing a MemoryStream is roughly the same thing as dropping a reference to a byte[] -- the GC will clean both up the same way.
You would use the FileStream to read/write a file but a MemoryStream to read/write in-memory data, such as a byte array decoded from a string. You would not use a Stream in and of itself, but rather use it for polymorphism, i.e. passing it to methods that can accept any implementation of Stream as an argument.
You have two options:
1-Encapsulate all the process inside a task:
while(...) {
var task = Task.Run(async () => {
var memoryStream = new MemoryStream(...);
var res = await _pageBlob.WritePagesAsync(memoryStream, ... );
memoryStream.Dispose();
});
tasks.Add(task);
}
2-Use a Continuation:
while(...) {
var memoryStream = new MemoryStream(...);
var task = _pageBlob.WritePagesAsync(memoryStream, ... )
.ContinueWith((PrevTask) => memoryStream.Dispose());
tasks.Add(task);
}
Split out your while
loop body into a separate async
method:
private async Task WriteAsync(...)
{
using (var memoryStream = new MemoryStream(...))
{
await _pageBlob.WritePagesAsync(memoryStream, ...);
}
}
Then use your new method:
public void Write() {
var tasks = new List<Task>();
while(...) {
tasks.Add(WriteAsync(...));
}
Task.WaitAll(tasks.ToArray());
}
On a side note, blocking on asynchronous code (Task.WaitAll
) is not generally a good idea. A more natural approach is to keep it asynchronous:
public async Task WriteAsync() {
var tasks = new List<Task>();
while(...) {
tasks.Add(WriteAsync(...));
}
await Task.WhenAll(tasks);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With