[Mono-dev] BackgroundWorker.CancelAsync() behaves incorrectly?

Jordan Callicoat MonkeeSage at gmail.com
Sat Mar 22 03:04:35 EDT 2008


Here is a simple test-case:

using System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections.Generic;

namespace TestCase
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
    class Form1 : Form
    {
        private Label label1;
        private Button button1;
        private Button button2;
        private BackgroundWorker backgroundWorker1;
        private int counter;
        private System.ComponentModel.IContainer components = null;
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
                components.Dispose();
            base.Dispose(disposing);
        }
        private void InitializeComponent()
        {
            this.label1 = new System.Windows.Forms.Label();
            this.button1 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.backgroundWorker1 = new
System.ComponentModel.BackgroundWorker();
            this.SuspendLayout();

            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(16, 17);
            this.label1.Margin = new System.Windows.Forms.Padding(4);
            this.label1.Name = "label1";
            this.label1.Text = "0";
            this.label1.Size = new System.Drawing.Size(36, 17);
            this.label1.TabIndex = 1;
            this.label1.Visible = true;

            this.button1.Location = new System.Drawing.Point(80, 12);
            this.button1.Margin = new System.Windows.Forms.Padding(4);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(91, 28);
            this.button1.TabIndex = 2;
            this.button1.Text = "Test";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.OnStartTest);
            this.button1.Visible = true;

            this.button2.Location = new System.Drawing.Point(176, 12);
            this.button2.Margin = new System.Windows.Forms.Padding(4);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(91, 28);
            this.button2.TabIndex = 2;
            this.button2.Text = "Stop";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.OnStopTest);
            this.button2.Visible = true;

            this.backgroundWorker1.WorkerSupportsCancellation = true;
            this.backgroundWorker1.DoWork +=
                new System.ComponentModel.DoWorkEventHandler(this.OnDoWork);
            this.backgroundWorker1.RunWorkerCompleted +=
                new System.ComponentModel.RunWorkerCompletedEventHandler(
                  this.OnWorkDone
                );

            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(290, 50);
            this.FormBorderStyle =
              System.Windows.Forms.FormBorderStyle.FixedDialog;
            this.Margin = new System.Windows.Forms.Padding(4);
            this.MaximizeBox = false;
            this.Name = "Form1";
            this.Text = "TestCase";
            this.Controls.Add(this.label1);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.button2);
            this.ResumeLayout(false);
            this.PerformLayout();
        }
        public Form1()
        {
            InitializeComponent();
        }
        private void OnStartTest(object sender, EventArgs e)
        {
            backgroundWorker1.RunWorkerAsync();
        }
        private void OnStopTest(object sender, EventArgs e)
        {
            backgroundWorker1.CancelAsync();
        }
        delegate void UpdateLabelCallback(string text);

        private void UpdateLabel(string text)
        {
            if (label1.InvokeRequired)
            {
                UpdateLabelCallback d = new
UpdateLabelCallback(UpdateLabel);
                this.Invoke(d, new object[] { text });
            }
            else
            {
                label1.Text = text;
            }
        }
        private void OnDoWork(object sender, EventArgs e)
        {
            counter = 0;
            BackgroundWorker bw = sender as BackgroundWorker;
            while (!bw.CancellationPending)
            {
                this.UpdateLabel(counter.ToString());
                Thread.Sleep(1000);
                ++counter;
            }
        }
        private void OnWorkDone(object sender, EventArgs e)
        {
            this.UpdateLabel("Done!");
        }
    }
}

-J
-- 
View this message in context: http://www.nabble.com/BackgroundWorker.CancelAsync%28%29-behaves-incorrectly--tp16216320p16217270.html
Sent from the Mono - Dev mailing list archive at Nabble.com.



More information about the Mono-devel-list mailing list