Youtube data API v3,progressBar上传进度

Youtube data API v3, upload progression in progressBar

我目前正在为学校项目开发软件。在这个软件中,我需要在 YouTube 上发布一个视频(使用 YouTube 数据 API v3),并在这样做的同时显示进度。每次进度更改时,我都会调用一个事件,并将其显示在控制台中。因为我的应用程序是 winForm,所以我需要在 progressBar(或类似的东西)中显示它。当我尝试这样做时,它给了我一个错误: "Cross-thread operation not valid: Control 'progressBar1' accessed from a thread other than the thread it was created on." 我已经尝试这样做了:

progressBar1.BeginInvoke(new MethodInvoker(delegate { progressBar1.Value = prog; }));

它没有给我报错,但它只显示最后的进度(当 100% 时),所以我需要一种在过程中显示的方法。

调用事件的代码如下:

            UserCredential credential;
            using (var stream = new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read))
            {
                credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.Load(stream).Secrets,
                    // This OAuth 2.0 access scope allows an application to upload files to the
                    // authenticated user's YouTube channel, but doesn't allow other types of access.
                    new[] { YouTubeService.Scope.YoutubeUpload },
                    "user",
                    CancellationToken.None
                );
            }

            var youtubeService = new YouTubeService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = Assembly.GetExecutingAssembly().GetName().Name
            });


            var video = new Video();
            video.Snippet = new VideoSnippet();
            video.Snippet.Title = title[i];
            video.Snippet.Description = description[i];

            string[] tags = tag[i].Split(',');
            video.Snippet.Tags = tags;

            video.Snippet.CategoryId = category[i]; // See https://gist.github.com/dgp/1b24bf2961521bd75d6c
            video.Status = new VideoStatus();
            video.Status.PrivacyStatus = privacy[i]; // or "private" or "public" or "unlisted"
            var filePath = vidpath[i]; // Replace with path to actual movie file.

            current_vid = i;

            using (var fileStream = new FileStream(filePath, FileMode.Open))
            {
                const int KB = 0x400;
                var minimumChunkSize = 256 * KB;

                var videosInsertRequest = youtubeService.Videos.Insert(video, "snippet,status", fileStream, "video/*");
                videosInsertRequest.ProgressChanged += videosInsertRequest_ProgressChanged;
                videosInsertRequest.ResponseReceived += videosInsertRequest_ResponseReceived;
                // The default chunk size is 10MB, here will use 1MB.
                videosInsertRequest.ChunkSize = minimumChunkSize * 4;
                videosInsertRequest.Upload();
            }

这是事件:

        switch (progress.Status)
        {
            case UploadStatus.Uploading:
                progressBar1.BeginInvoke(new MethodInvoker(delegate { progressBar1.Value = Convert.ToInt32(progress.BytesSent * 100 / total_size); }));
                Console.WriteLine(progress.BytesSent * 100 / total_size);
                break;

            case UploadStatus.Failed:
                Console.WriteLine("An error prevented the upload from completing.\n{0}", progress.Exception);
                break;
        }

我能够纠正这个问题的唯一方法是创建一个控制台应用程序,并在我想给出进度时显示控制台。

感谢您的支持。