它沒有 IsBusy, WorkComplete 事件等,要自己做,但我覺得比較直觀,貌似對 async 的支援也好點。
public partial class Form1 : Form { IProgress<int> _progress; public Form1() { InitializeComponent(); _progress = new Progress<int>(UpdateProgressBar); } private void UpdateProgressBar(int val) { pbProgress.Value = val; if (val == 100) { MessageBox.Show("Finished"); pbProgress.Value = 0; } } private async void btnStart_Click(object sender, EventArgs e) { if (pbProgress.Value == 0) { await Task.Run(async () => { for (int i = 0; i < 100; i++) { _progress.Report(i + 1); await Task.Delay(100 - i); } }); } } }
ref: GitHub 範例(Form2是 backgroundworker,可以在 program 切換)
沒有留言:
張貼留言