Asynchronously download Google files using the WPF runtime

I am logging into Google Cloud Storage using a service account. I need to be able to display the download progress in the WPF user interface. Now when I try to update ProgressBar.Value, it does not work, but when I just have a bytesSent written in the console, I can see the progress.

public async Task<bool> UploadToGoogleCloudStorage(string bucketName, string token, string filePath, string contentType) { var newObject = new Google.Apis.Storage.v1.Data.Object() { Bucket = bucketName, Name = System.IO.Path.GetFileNameWithoutExtension(filePath) }; var service = new Google.Apis.Storage.v1.StorageService(); try { using (var fileStream = new FileStream(filePath, FileMode.Open)) { var uploadRequest = new ObjectsResource.InsertMediaUpload(service, newObject, bucketName, fileStream, contentType); uploadRequest.OauthToken = token; ProgressBar.Maximum = fileStream.Length; uploadRequest.ProgressChanged += UploadProgress; uploadRequest.ChunkSize = (256 * 1024); await uploadRequest.UploadAsync().ConfigureAwait(false); service.Dispose(); } } catch (Exception ex) { Console.WriteLine(ex.Message); throw ex; } return true; } private void UploadProgress(IUploadProgress progress) { switch (progress.Status) { case UploadStatus.Starting: ProgressBar.Minimum = 0; ProgressBar.Value = 0; break; case UploadStatus.Completed: System.Windows.MessageBox.Show("Upload completed!"); break; case UploadStatus.Uploading: //Console.WriteLine(progress.BytesSent); -> This is working if I don't call the method below. UpdateProgressBar(progress.BytesSent); break; case UploadStatus.Failed: Console.WriteLine("Upload failed " + Environment.NewLine + progress.Exception.Message + Environment.NewLine + progress.Exception.StackTrace + Environment.NewLine + progress.Exception.Source + Environment.NewLine + progress.Exception.InnerException + Environment.NewLine + "HR-Result" + progress.Exception.HResult); break; } } private void UpdateProgressBar(long value) { Dispatcher.Invoke(() => { this.ProgressBar.Value = value; }); } 
+5
source share

Source: https://habr.com/ru/post/1276061/


All Articles