尝试从backgroundWorker1_ProgressChanged
更新列表框,但backgroundWorker1_DoWork
完成其过程时会这样做。我想在backgroundWorker1_DoWork
仍在执行之前更新列表框,并定期调用backgroundWorker1.ReportProgress
并更新列表框。
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
// loops
backgroundWorker1.ReportProgress(1, list);// sending list to listbox
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
dynamic a = e.UserState;
BeginInvoke((MethodInvoker)delegate
{ listbox1.Items.Add(a[0]); });
// updates after backgroundWorker1_DoWork is executed?
}
答案 0 :(得分:0)
答案:请添加延迟
private void OnDataReceived(object sender, DataReceivedEventArgs e) {
if (!String.IsNullOrEmpty(e.Data)) {
backgroundWorker1.ReportProgress(1, e.Data);
System.Threading.Thread.Sleep(300);
}
}