更换每个文件的背景工作者标签

时间:2014-01-26 02:30:27

标签: c# backgroundworker

我有一个后台工作程序,它检查文件列表中的字节大小和MD5哈希值。当它检查它们时,我想要一个标签来说Checking 'FileName'

这是我改进的进度事件:

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        var fileName = Convert.ToString(e.UserState);

        lblCheckFile.Text = string.Format("Downloading {0}", fileName);
        pBarFileProgress.Value = e.ProgressPercentage;
        gbFileProgress.Text = String.Format("Current File Progress: {0} %", e.ProgressPercentage);
    }

我有downloading说实际下载时间,但我希望它说Checking,并为此提供响应式进度条。在下载之前。

1 个答案:

答案 0 :(得分:2)

您可以在下载时调用文件旁边调用Progress Changed事件。但是你需要通过UserState对象传递更多信息,例如说明除了文件名信息之外是“下载”还是“正在检查”。

backgroundWorker.ReportProgress(0.5, new {state = "Checking", fileName = "SomeFile.jpg"});
//and later when downloading :
//backgroundWorker.ReportProgress(0.5, new {state = "Downloading", fileName = "SomeFile.jpg"});

然后在事件处理程序中:

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    var userState = (dynamic)e.UserState;
    var state = userState.state;
    var fileName = userState.fileName

    lblCheckFile.Text = string.Format("{0} {1}", state, fileName);
    pBarFileProgress.Value = e.ProgressPercentage;
    gbFileProgress.Text = String.Format("Current File Progress: {0} %", e.ProgressPercentage);
}