下面是示例代碼,它使用devexpress bareditItem(progressbar)來顯示加載數據時的進度。我想知道是否有一種方法可以使用異步等待和任務顯示相同的進度條(顯示數據加載時的進度)。後臺工作者異步等待任務
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace ProgressBar {
public partial class Form1 : DevExpress.XtraEditors.XtraForm {
DataTable workTable;
public Form1() {
InitializeComponent();
workTable = new DataTable("Records");
workTable.Columns.Add("Id", typeof(int));
workTable.Columns.Add("Data", typeof(String));
}
//this data varies from 0 to 50,000 rows
private void LoadData(DoWorkEventArgs e) {
for(int i = 0; i < 1001; i++) {
System.Threading.Thread.Sleep(5);
workTable.Rows.Add(i, String.Format("Record {0}", i));
this.backgroundWorker1.ReportProgress(i, i);
}
}
private void button1_Click(object sender, EventArgs e) {
this.backgroundWorker1.RunWorkerAsync();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) {
LoadData(e);
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) {
DataTable up = workTable.Clone();
this.barEditItem1.EditValue = e.ProgressPercentage;
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
gridControl1.DataSource = workTable;
}
}
}
BackgroundWorker沒有過時。 – LarsTech
@LarsTech不推薦在異步等待發布後使用。 – user3185569