Example #1
0
        public async Task <Stream> ReadFileAsync(string share, string filename, Stream stream, CancellationToken token = default(CancellationToken))
        {
            if (string.IsNullOrEmpty(share))
            {
                throw new ArgumentNullException("share");
            }

            if (string.IsNullOrEmpty("filename"))
            {
                throw new ArgumentNullException("filename");
            }

            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            Exception error = null;
            Stopwatch watch = new Stopwatch();

            watch.Start();
            double time             = watch.Elapsed.TotalMilliseconds;
            long   bytesTransferred = 0;



            try
            {
                CloudFileShare     choudShare = client.GetShareReference(share);
                CloudFileDirectory dir        = choudShare.GetRootDirectoryReference();
                CloudFile          file       = dir.GetFileReference(filename);

                IProgress <StorageProgress> progressHandler = new Progress <StorageProgress>(
                    progress =>
                {
                    bytesTransferred = bytesTransferred < progress.BytesTransferred ? progress.BytesTransferred : bytesTransferred;
                    if (watch.Elapsed.TotalMilliseconds > time + 1000.0 && bytesTransferred <= progress.BytesTransferred)
                    {
                        OnDownloadBytesTransferred?.Invoke(this, new BytesTransferredEventArgs(share, filename, bytesTransferred, file.Properties.Length));
                    }
                });

                await file.DownloadToStreamAsync(stream, default(AccessCondition), default(FileRequestOptions), default(OperationContext), progressHandler, token);
            }
            catch (Exception ex)
            {
                if (ex.InnerException is TaskCanceledException)
                {
                    stream = null;
                }
                else
                {
                    error = ex;
                    throw ex;
                }
            }
            finally
            {
                watch.Stop();
                OnDownloadCompleted?.Invoke(this, new BlobCompleteEventArgs(share, filename, token.IsCancellationRequested, error));
            }

            return(stream);
        }
Example #2
0
 private void Remote_OnDownloadBytesTransferred(object sender, BytesTransferredEventArgs e)
 {
     OnDownloadBytesTransferred?.Invoke(this, e);
 }