我在form1顶部添加了一个字符串数组:
string[] stringProgressReport;
然后我做了一件事:
private void videosInsertRequest_ResponseReceived(Video obj)
{
string r1 = stringProgressReport[0] = obj.Status.UploadStatus;
backgroundWorker1.ReportProgress(0, r1);
}
然后在第二次事件中我做了:
private void videosInsertRequest_ProgressChanged(IUploadProgress obj)
{
string r2 = stringProgressReport[1] = obj.Status.ToString();
backgroundWorker1.ReportProgress(0, r2);
}
然后在backgroundworker progresschanged事件:
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
toolStripStatusLabel1.Text =
toolStripStatusLabel2.Text =
}
Label1我想获取第一个数组报告索引[0]的e.UserState和索引[1]中的第二个Label2。
e.UserState没有任何索引选项可供选择。
修改
在我的DoWork活动中,我已经准备好了:
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
UploadVideo(FileNameToUpload, uploadVideoTitleTxtBox.Text, uploadVideoDescriptionTxtBox.Text);
}
上传视频方法:
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();
}
}
从这个UploadVideo我正在注册两个事件:videosInsertRequest_ResponseReceived和videosInsertRequest_ProgressChanged,我在我的问题的顶部显示。
从这两个事件我想报告后台工作者的progresschanged事件。
我现在在DoWork事件和progresschanged事件中所做的是:
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
UploadVideo(FileNameToUpload, uploadVideoTitleTxtBox.Text, uploadVideoDescriptionTxtBox.Text);
for (int i = 0; i < stringProgressReport.Length; i++)
{
backgroundWorker1.ReportProgress(i, stringProgressReport);
}
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
string[] labelsreports = (string[])e.UserState;
toolStripStatusLabel1.Text = labelsreports[0];
toolStripStatusLabel2.Text = labelsreports[1];
}
我没有收到任何错误或异常,但没有更新toolStripStatusLabel1和2.
答案 0 :(得分:1)
如果我正确理解了这个问题,那么您有一个后台工作线程正在执行代码,该代码可以引发您要报告进度的两种不同类型的事件。 ProgressChanged
事件处理程序反过来需要某种方式来更新给定字符串的相应标签。
在我看来,让代码工作的最简单方法是只更新两个标签,而不管发生了哪些基础事件:
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
toolStripStatusLabel1.Text = stringProgressReport[0];
toolStripStatusLabel2.Text = stringProgressReport[1];
}
您可能希望将stringProgressReport
元素初始化为合理的内容,以便在实际引发与该索引相对应的事件之前,仍会显示一些合理的文本(因此您不会分配{{ 1}}到null
属性。)
如果您想要更多&#34;手术&#34;,您可以将相应的索引作为Text
参数传递(而不是像现在那样传递userState
):
string
这将允许private void videosInsertRequest_ResponseReceived(Video obj)
{
stringProgressReport[0] = obj.Status.UploadStatus;
backgroundWorker1.ReportProgress(0, 0);
}
private void videosInsertRequest_ProgressChanged(IUploadProgress obj)
{
stringProgressReport[1] = obj.Status.ToString();
backgroundWorker1.ReportProgress(0, 1);
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
int eventIndex = (int)e.UserState;
if (eventIndex == 0)
{
toolStripStatusLabel1.Text = stringProgressReport[0];
}
else
{
toolStripStatusLabel2.Text = stringProgressReport[1];
}
}
事件处理程序仅更新与该特定更新相关的标签。
当然,您可以采取许多其他方法同样有效,包括根本不需要使用ProgressChanged
数组的变体。但是上述任何一个选项都适合您,并且可以让您更好地了解这些事件处理程序如何协同工作以实现您想要的效果(即,您可以根据需要实现其他变体)。