using System; using System.Collections; using System.ComponentModel; using System.Drawing; using System.Threading; using System.Windows.Forms; namespace BackgroundWorkerExample { public class FibonacciForm : System.Windows.Forms.Form { private int numberToCompute = 0; private int highestPercentageReached = 0; private System.Windows.Forms.NumericUpDown numericUpDown1; private System.Windows.Forms.Button startAsyncButton; private System.Windows.Forms.Button cancelAsyncButton; private System.Windows.Forms.ProgressBar progressBar1; private System.Windows.Forms.Label resultLabel; private System.ComponentModel.BackgroundWorker backgroundWorker1; public FibonacciForm() { InitializeComponent(); InitializeBackgoundWorker(); } private void InitializeBackgoundWorker() { backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork); backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted); backgroundWorker1.ProgressChanged +=new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged); } private void startAsyncButton_Click(System.Object sender, System.EventArgs e) { resultLabel.Text = String.Empty; this.numericUpDown1.Enabled = false; this.startAsyncButton.Enabled = false; this.cancelAsyncButton.Enabled = true; numberToCompute = (int)numericUpDown1.Value; highestPercentageReached = 0; backgroundWorker1.RunWorkerAsync(numberToCompute); } private void cancelAsyncButton_Click(System.Object sender,System.EventArgs e) { this.backgroundWorker1.CancelAsync(); cancelAsyncButton.Enabled = false; } private void backgroundWorker1_DoWork(object sender,DoWorkEventArgs e) { BackgroundWorker worker = sender as BackgroundWorker; e.Result = Sum((int)e.Argument, worker, e); } private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { if (e.Error != null) { MessageBox.Show(e.Error.Message); } else if (e.Cancelled) { resultLabel.Text = "Canceled"; } else { resultLabel.Text = e.Result.ToString(); } this.numericUpDown1.Enabled = true; startAsyncButton.Enabled = true; cancelAsyncButton.Enabled = false; } private void backgroundWorker1_ProgressChanged(object sender,ProgressChangedEventArgs e) { this.progressBar1.Value = e.ProgressPercentage; } long Sum(int n, BackgroundWorker worker, DoWorkEventArgs e) { long result = 0; if (worker.CancellationPending) { e.Cancel = true; } else { for (int i = 1; i <= n; i++) { result = result + i; int percentComplete =(int)((float)i / (float)numberToCompute * 100); if (percentComplete > highestPercentageReached) { highestPercentageReached = percentComplete; worker.ReportProgress(percentComplete); } } } return result; } #region Windows Form Designer generated code private void InitializeComponent() { this.numericUpDown1 = new System.Windows.Forms.NumericUpDown(); this.startAsyncButton = new System.Windows.Forms.Button(); this.cancelAsyncButton = new System.Windows.Forms.Button(); this.resultLabel = new System.Windows.Forms.Label(); this.progressBar1 = new System.Windows.Forms.ProgressBar(); this.backgroundWorker1 = new System.ComponentModel.BackgroundWorker(); ((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).BeginInit(); this.SuspendLayout(); // // numericUpDown1 // this.numericUpDown1.Location = new System.Drawing.Point(16, 16); this.numericUpDown1.Maximum = new decimal(new int[] { 1215752191, 23, 0, 0}); this.numericUpDown1.Minimum = new decimal(new int[] { 1, 0, 0, 0}); this.numericUpDown1.Name = "numericUpDown1"; this.numericUpDown1.Size = new System.Drawing.Size(80, 21); this.numericUpDown1.TabIndex = 0; this.numericUpDown1.Value = new decimal(new int[] { 10, 0, 0, 0}); // // startAsyncButton // this.startAsyncButton.Location = new System.Drawing.Point(18, 64); this.startAsyncButton.Name = "startAsyncButton"; this.startAsyncButton.Size = new System.Drawing.Size(120, 23); this.startAsyncButton.TabIndex = 1; this.startAsyncButton.Text = "Start Async"; this.startAsyncButton.Click += new System.EventHandler(this.startAsyncButton_Click); // // cancelAsyncButton // this.cancelAsyncButton.Enabled = false; this.cancelAsyncButton.Location = new System.Drawing.Point(155, 64); this.cancelAsyncButton.Name = "cancelAsyncButton"; this.cancelAsyncButton.Size = new System.Drawing.Size(119, 23); this.cancelAsyncButton.TabIndex = 2; this.cancelAsyncButton.Text = "Cancel Async"; this.cancelAsyncButton.Click += new System.EventHandler(this.cancelAsyncButton_Click); // // resultLabel // this.resultLabel.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; this.resultLabel.Location = new System.Drawing.Point(112, 16); this.resultLabel.Name = "resultLabel"; this.resultLabel.Size = new System.Drawing.Size(160, 23); this.resultLabel.TabIndex = 3; this.resultLabel.Text = "(no result)"; this.resultLabel.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // // progressBar1 // this.progressBar1.Location = new System.Drawing.Point(18, 48); this.progressBar1.Name = "progressBar1"; this.progressBar1.Size = new System.Drawing.Size(256, 10); this.progressBar1.Step = 2; this.progressBar1.TabIndex = 4; // // backgroundWorker1 // this.backgroundWorker1.WorkerReportsProgress = true; this.backgroundWorker1.WorkerSupportsCancellation = true; // // FibonacciForm // this.ClientSize = new System.Drawing.Size(292, 107); this.Controls.Add(this.progressBar1); this.Controls.Add(this.resultLabel); this.Controls.Add(this.cancelAsyncButton); this.Controls.Add(this.startAsyncButton); this.Controls.Add(this.numericUpDown1); this.Name = "FibonacciForm"; this.Text = "Sum"; this.Load += new System.EventHandler(this.FibonacciForm_Load); ((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).EndInit(); this.ResumeLayout(false); } #endregion [STAThread] static void Main() { Application.Run(new FibonacciForm()); } private void FibonacciForm_Load(object sender, EventArgs e) { } } } |
BackgroundWork类使用DEMO
最新推荐文章于 2023-06-06 09:24:19 发布
BackgroundWork类使用DEMO