如何使用BackgroundWorker.ReportProgress WPF发送多个UserState

时间:2016-08-17 05:14:10

标签: c# wpf backgroundworker

如何使用BackgroundWorker.ReportProgress发送多个UserState?

例如:

progressbar.Value = e.UserState as double; 

这个将用于ProgessBar

lblpercent.Content = e.UserState as string;

这个将用于标签

谢谢

2 个答案:

答案 0 :(得分:2)

您可以将进度传递给percentProgress参数

worker.ReportProgress(55,"test");

progressbar.Value = e.PercentProgress / 100.0 * progressbar.MaxValue;
lblpercent.Content = e.UserState.ToString();

或通过userState

传递一个类/结构
class MyUserState
{
    public double Value {get;set;}
    public string Content {get;set;}
}

worker.ReportProgress(55,new MyUserState { Value = 23.7, Content = "Working ..."});

MyUserState state = e.UserState as MyUserState;
progressbar.Value = state.Value;
lblpercent.Content = state.Content;

答案 1 :(得分:0)

我确实喜欢这个backgroundWorker1_DoWork

string [] workerResult = new string[3];
workerResult[0] = total.ToString();
orkerResult[1] = "Files Copied: " + i + "/" + filecount;
workerResult[2] = "Size Copied: " + totalmb + "/" + totalsize + " MB";

并进入backgroundWorker1_ProgressChanged我说:

string[] results = (string[])e.UserState;
progressbar.Value = double.Parse(results[0]);
lblcopiedfiles.Content = results[1];
lblsizecopied.Content = results[2];

感谢您的帮助