我需要在DoWork事件之外更改背景工作进度,这可能吗?更改Backgroundworker从其他功能进展
我的代码
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";
}
你是什么意思? – Tigran 2012-04-19 15:11:55
你能详细说明你为什么要这样做吗?是否有工作在两个不同的地方进行,以便后台工作人员最终可以少做**工作? – mgnoonan 2012-04-19 15:12:04
正如你可以在我的代码中看到的,我启动事件worker_Dowork,并在其中我实例一个webclient,这个webclient报告进度progress_changed,但如果我更改我的主窗体ListView项目我得到crossthread异常,所以只有webclient事件报告他唯一的进度,然后后台工作进度事件更改列表视图项目。但为了提高背景工作进度事件,我需要更改webcliente进度事件中的背景工作进度。 – 2012-04-19 15:15:13