如何使用BackgroundWorker.ReportProgress发送多个UserState?
例如:
progressbar.Value = e.UserState as double;
这个将用于ProgessBar
lblpercent.Content = e.UserState as string;
这个将用于标签
谢谢
答案 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];
感谢您的帮助