1

我需要在 DoWork 事件之外更改我的后台工作人员进度,这可能吗?

我的代码

private void progress_changed(string fileName, DownloadProgressChangedEventArgs e)
{
    progress = e.ProgressPercentage;
    Filename = fileName;
    // change the progress here
}

private void worker_DoWork(string fileName)
{
    WebClient client = new WebClient();
    client.DownloadProgressChanged += (obj, e) => progress_changed(fileName, e);
    client.DownloadFileCompleted += new AsyncCompletedEventHandler(progress_complete);
    client.DownloadFileAsync(new Uri(Link), Savepath);
}

private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    mainForm.dlist.Items[mainForm.dlist.Items.IndexOfKey(Filename)].SubItems[2].Text = progress.ToString() + "%";
    mainForm.dlist.Items[mainForm.dlist.Items.IndexOfKey(Filename)].SubItems[3].Text = "Downloading";
}
4

1 回答 1

0

将 BackgroundWorker 传递给另一个函数。从那里,您可以调用ReportProgress. 或者,根据您的结构,您可以将 BackgroundWorker 设为类字段并以这种方式访问​​它。

于 2012-04-19T15:14:37.843 回答