Winform程序我在
private void button1_Click(object sender, EventArgs e)
{
}
中启动一个线程A,在线程中创建一个窗体Form1, 窗体上有一个按钮btnStop。启动线程A后,我在button1_Click种执行后面的一些处理。这时我如何通过点击按钮btnStop,来停止button1_Click的执行。
private void button1_Click(object sender, EventArgs e)
{
}
中启动一个线程A,在线程中创建一个窗体Form1, 窗体上有一个按钮btnStop。启动线程A后,我在button1_Click种执行后面的一些处理。这时我如何通过点击按钮btnStop,来停止button1_Click的执行。
解决方案 »
- C# InitializeWinIo()初始化返回false (Win7系统)
- 请教怎么将某一个子窗体置底(不要挡住了其它的子窗体)
- listbox中如何显示两行的子项
- 关于封装的问题
- 超级难题:.net 中CreateFileMapping 创建共享内存问题
- 向listview中的每个subItem插入数据应该用什么办法呢?
- CAD二次开发达人入,有谁能教教我这个函数怎么用?
- 如何让c#文本框拥有联想功能
- 急!好几天都没解决的问题。有用DevExpress的TreeList的朋友么,Tree只能显示一列,到底是Bug还是什么其他原因。如解决的话百分相送,Up
- 如何把字串转化成为字符串数组?1
- C#程序员看你现在到底了解多少?
- 怎样实现一个浮动的广告页啊?
2.将该线程直接abort不知道楼下还有什么别的好方法
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;
}
}
}
2、如果线程是在处理对象中,则在该对象中对外暴露STOP线程接口。