-1

I have this progress changed event:

private void videosInsertRequest_ProgressChanged(IUploadProgress obj)
        {

                toolStripStatusLabel1.Text = obj.Status.ToString();
        }

And also this event:

private void videosInsertRequest_ResponseReceived(Video obj)
        {
            toolStripStatusLabel1.Text = obj.Status.UploadStatus;
        }

And sometimes i'm getting the exception:

Cross-thread operation not valid: Control '' accessed from a thread other than the thread it was created on

Should i use a BeginInvoke over this control or to handle it in another place out of the event ?

This is where i register the event:

static Video video = null;
        static ulong process = 0;
        private void UploadVideo(string FileName, string VideoTitle, string VideoDescription)
        {
            UserCredential credential;
            using (FileStream stream = new FileStream(@"D:\C-Sharp\Youtube-Manager\Youtube-Manager\Youtube-Manager\bin\Debug\client_secrets.json", FileMode.Open, FileAccess.Read))
            {
                credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.Load(stream).Secrets,
                    new[] { YouTubeService.Scope.Youtube, YouTubeService.Scope.YoutubeUpload },
                    "user",
                    CancellationToken.None,
                    new FileDataStore("YouTube.Auth.Store")).Result;
            }
            var youtubeService = new YouTubeService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = Assembly.GetExecutingAssembly().GetName().Name
            });
            video = new Video();
            video.Snippet = new VideoSnippet();
            video.Snippet.Title = VideoTitle;
            video.Snippet.Description = VideoDescription;
            video.Snippet.Tags = new string[] { "tag1", "tag2" };
            comboBox1.BeginInvoke((Action)(() =>
            {
                video.Snippet.CategoryId = (comboBox1.SelectedItem as ComboboxItem).Value.ToString();
            }));
            video.Status = new VideoStatus();
            video.Status.PrivacyStatus = "public";

            using (var fileStream = new FileStream(FileName, 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;
                videosInsertRequest.ChunkSize = minimumChunkSize * 4;                
                videosInsertRequest.Upload();
            }
        }

And then i have a button click event where i start a backgroundworker runasync. And then in the DoWork event i'm doing:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            UploadVideo(FileNameToUpload, uploadVideoTitleTxtBox.Text, uploadVideoDescriptionTxtBox.Text);
        }

And now the backgroundworker progresschanged event is empty but somehow i should report to the backgroundworker form inside the two events in my top of my question.

Or maybe using BeginInvoke in this two events ?

I want to report to the toolStripStatusLabel1 and 2 from this events.

Simon Gamlieli
  • 119
  • 1
  • 18

1 Answers1

2

The ProgressChanged is a message system intended to get messages from the background worker thread to the calling thread. Since you have access to this plumbing, I would put the code in the ProgressChanged event by defining a message. You can pass multiple types of messages and arguments using this system.

The benefit to this is that your background worker thread stays within the confines of what it is intended for. If you decided to use the Invoke route, then you wouldn't have needed a background worker, you could have just used a thread object without the additional plumbing overhead.

Marc Johnston
  • 1,276
  • 1
  • 7
  • 16