I'm developing an application on 'VisualStudio 2017', 'C# 6.0', 'R#'. In C# 6.0 was introduced new feature - support async/await' withintry/cath'. i use using operator (which is really try/finaly under the hood), but i still receive warning Access to disposed closure. But really msGet shouldn't not be disposed before DownloadToStreamAsync.
- Does it can really access disposed variable?
- Is it
R#glitch?
my code:
using( var msGet = new MemoryStream() )
{
var stopwatchAp = Stopwatch.StartNew();
var stopwatchPureDownload = new Stopwatch();
var times = new long[ 201 ];
var retryCount = -1;
await this._ap.Do( () =>
{
stopwatchPureDownload.Restart();
var downloadToStreamAsync = blob.DownloadToStreamAsync( msGet );
downloadToStreamAsync.ContinueWith( t => times[ ++retryCount ] = stopwatchPureDownload.ElapsedMilliseconds );
return downloadToStreamAsync;
} ).ConfigureAwait( false );
}
Update 1 I have seen similar questions before:
Access to disposed closure in C#?
(but example here doesn't contains await, it is not about 'async/await')
Calling asynchronous method in using statement .
(the answer says - useawait. But as you can see my code contains await- but i still have the warning )
Also these questions dated to 2013 year. There was no C# 6.0 in 2013 year. That's why I'm asking.
msSetwill not be disposed yet when async block runs. It just sees that you captured and use some variable which is later disposed (not related to async at all).Dodo? Who says that it executes the lambda before returning? There is no guarantee thatmsGetwill be available when the code actually executes.Domay be storing it for later execution.