public partial class Form1 : Form
{
AutoResetEvent are = new AutoResetEvent(false);//设置自动重置事件默认值为非终止状态
public Form1()
{
InitializeComponent();
TextBox.CheckForIllegalCrossThreadCalls = false;//关闭文本框的跨线程操作检查
}
private void button1_Click(object sender, EventArgs e)
{
Thread t = new Thread(fun);
t.IsBackground = true;
t.Start();
}
//在线程中执行的方法
private void fun()
{
int i = 0;
while (i<9999)
{
i += 1;
textBox2.Text = i.ToString();
if (textBox1.Text =="false")
{
are.WaitOne();//阻塞当前线程
}
}
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text = "false";
}
private void button3_Click(object sender, EventArgs e)
{
textBox1.Text = "true";
are.Set();//释放所有被阻塞的线程
}
}
{
AutoResetEvent are = new AutoResetEvent(false);//设置自动重置事件默认值为非终止状态
public Form1()
{
InitializeComponent();
TextBox.CheckForIllegalCrossThreadCalls = false;//关闭文本框的跨线程操作检查
}
private void button1_Click(object sender, EventArgs e)
{
Thread t = new Thread(fun);
t.IsBackground = true;
t.Start();
}
//在线程中执行的方法
private void fun()
{
int i = 0;
while (i<9999)
{
i += 1;
textBox2.Text = i.ToString();
if (textBox1.Text =="false")
{
are.WaitOne();//阻塞当前线程
}
}
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text = "false";
}
private void button3_Click(object sender, EventArgs e)
{
textBox1.Text = "true";
are.Set();//释放所有被阻塞的线程
}
}