BackgroundWorkerを使って実行中の
別スレッドの進捗状況を知らせる方法です。
別節のサンプルに少し手を加えています。
具体的には
ProgressChangedハンドラを追加して進捗の変化を捕捉します。
なお、WorkerReportsProgressプロパティーにtrueを設定して
進捗報告を許可するのを忘れずに
進捗状況の設定は
ReportProgress関数で行います。引数は0~100%
下の例では視覚的に進捗状況がわかるように
ProgressBarコントロールをFormに追加しています。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { // RunWorkerAsyncが呼び出された時に発生 backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork); // BackgroundWorkerでの処理が完了した時に発生 backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted); // 進捗があった時に発生 backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged); // キャンセルされることを許可 backgroundWorker1.WorkerSupportsCancellation = true; // 進捗報告を許可 backgroundWorker1.WorkerReportsProgress = true; } void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) { progressBar1.Value = e.ProgressPercentage; } void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { if(e.Cancelled) MessageBox.Show("処理はキャンセルされました"); else MessageBox.Show("処理完了"); } void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { for (int i = 0; i < 100; i++) { // キャンセルされた? if (backgroundWorker1.CancellationPending) { e.Cancel = true; return; } backgroundWorker1.ReportProgress(i); System.Diagnostics.Debug.Write("@"); Application.DoEvents(); } backgroundWorker1.ReportProgress(100); } private void button1_Click(object sender, EventArgs e) { // BackgroundWorkerが処理中でなければ if (!backgroundWorker1.IsBusy) { // バックグラウンド処理を開始 backgroundWorker1.RunWorkerAsync(); for (int i = 0; i < 100; i++) { System.Diagnostics.Debug.Write("_"); Application.DoEvents(); } } } private void button2_Click(object sender, EventArgs e) { // 処理をキャンセルします backgroundWorker1.CancelAsync(); } } }
コメントを残す