Winform程序我在
private void button1_Click(object sender, EventArgs e)
{
}
中启动一个线程A,在线程中创建一个窗体Form1, 窗体上有一个按钮btnStop。启动线程A后,我在button1_Click种执行后面的一些处理。这时我如何通过点击按钮btnStop,来停止button1_Click的执行。

解决方案 »

  1.   

    1.设置一个变量,在button1_Click执行的过程中,检测该变量的值,如果状态不对就return
    2.将该线程直接abort不知道楼下还有什么别的好方法
      

  2.   

    为楼上补充代码 ^_^
    using System;
    using System.Windows.Forms;
    using System.Threading;
    namespace WindowsApplication1
    {
    public class Common
    {
    public static bool ifStart = false;
    } public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.Label label1;
    private System.Threading.Thread myThread;
    private System.ComponentModel.Container components = null; public Form1()
    { InitializeComponent();
    } protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    }
    private void InitializeComponent()
    {
    this.button1 = new System.Windows.Forms.Button();
    this.label1 = new System.Windows.Forms.Label();
    this.SuspendLayout();
    this.button1.Location = new System.Drawing.Point(24, 72);
    this.button1.Name = "button1";
    this.button1.Size = new System.Drawing.Size(72, 24);
    this.button1.TabIndex = 0;
    this.button1.Text = "Start";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    this.label1.Location = new System.Drawing.Point(24, 16);
    this.label1.Name = "label1";
    this.label1.Size = new System.Drawing.Size(264, 40);
    this.label1.TabIndex = 1;
    this.label1.Text = "label1";
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(292, 273);
    this.Controls.Add(this.label1);
    this.Controls.Add(this.button1);
    this.Name = "Form1";
    this.Text = "Form1";
    this.ResumeLayout(false);
    } [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    } private void button1_Click(object sender, System.EventArgs e)
    {
    Common.ifStart = true;
    Form2 f2 = new Form2();
    f2.Show();
    ThreadStart myThreadDelegate = new ThreadStart(DoWork);
    myThread = new Thread(myThreadDelegate);
    myThread.Start();
    } private void DoWork()
    {
    for(int i = 1; i<= 3;i++)
    {
    if(Common.ifStart == false)
    {
    myThread.Abort();
    return;
    } if(i%3 == 1)
    this.label1.Text = "Working thread.";
    else if(i%3 == 2)
    this.label1.Text = "Working thread..";
    else
    {
    this.label1.Text = "Working thread...";
    i = 0;
    }
    Thread.Sleep(100);
    } }
    }
    public class Form2 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Button button1;
    private System.ComponentModel.Container components = null; public Form2()
    {
    InitializeComponent();
    }
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if(components != null)
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    }
    private void InitializeComponent()
    {
    this.button1 = new System.Windows.Forms.Button();
    this.SuspendLayout();
    this.button1.Location = new System.Drawing.Point(88, 48);
    this.button1.Name = "button1";
    this.button1.TabIndex = 0;
    this.button1.Text = "Stop";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(292, 273);
    this.Controls.Add(this.button1);
    this.Name = "Form2";
    this.Text = "Form2";
    this.ResumeLayout(false);
    } private void button1_Click(object sender, System.EventArgs e)
    {
    Common.ifStart = false;
    }
    }
    }
      

  3.   

    1、如果线程是在form中定义的,那么可在STOP时中止线程,但是要截取ThreadAbortException,因为在中止线程时始终都会抛出该异常
    2、如果线程是在处理对象中,则在该对象中对外暴露STOP线程接口。