I am trying to download a blob from Azure with a fixed timeout. I have the following working code in .NET 4.5. But, when I try to rewrite in .NET 4.0, I could not find a way to specify the timeout for CancellationTokenSource. Can you please help?
var cts = new CancellationTokenSource((int)TimeSpan.FromSeconds(30).TotalMilliseconds);
using (var memoryStream = new System.IO.MemoryStream())
{
Task task = blockBlob.DownloadToStreamAsync(memoryStream, cts.Token);
await task.ConfigureAwait(false);
...
}
Additionally, I found the following code (in 4.0) to timeout if the blob is not downloaded in the specified time. I am not sure if there is anything I should be careful in using it.
Task task = blockBlob.DownloadToStreamAsync(memoryStream);
task.Wait((int)TimeSpan.FromSeconds(30).TotalMilliseconds);