线程初识,为什么Abort()了不能再start
2011-03-22 17:38
337 查看
2011-03-22
今天第一天学习线程知识,顺便来做下笔记~~
以下是代码:
using System;
using System.Windows.Forms;
using System.Threading;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private Thread thread1 = new Thread(new ThreadStart(FormShow1));
public Form1()
{
InitializeComponent();
}
private void Threa1Start_Click(object sender, EventArgs e)
{
thread1.Start();
buttom();
textBox1.Text += "thread1.IsAlive:" + thread1.IsAlive + "/r/n";
}
private void Threa1Stop_Click(object sender, EventArgs e)
{
thread1.Abort();
thread1.Join();
FormShowStop1();
buttom();
textBox1.Text += "thread1.IsAlive:" + thread1.IsAlive + "/r/n";
}
private static void FormShow1()
{
MessageBox.Show("线程1启动");
}
private void FormShowStop1()
{
MessageBox.Show("线程1停止");
}
private void buttom()
{
if (thread1.ThreadState == ThreadState.Running)
{
Threa1Stop.Enabled = true;
Threa1Start.Enabled = false;
}
else
{
Threa1Stop.Enabled = false;
Threa1Start.Enabled = true;
}
}
}
}
以上代码初次运行没问题,但点击Threa1Stop停止后,再点击开始就没办了~~~问了一下google~
原来每次线程结束了,需要重新再new一次才可以正常进行~~~
于是改成:
private Thread thread1 = null;
再把
private void Threa1Start_Click(object sender, EventArgs e)
{
this.thread1 =new Thread(new ThreadStart(FormShow1));
thread1.Start();
buttom();
textBox1.Text += "thread1.IsAlive:" + thread1.IsAlive + "/r/n";
}
再运行,OK~~~
今天第一天学习线程知识,顺便来做下笔记~~
以下是代码:
using System;
using System.Windows.Forms;
using System.Threading;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private Thread thread1 = new Thread(new ThreadStart(FormShow1));
public Form1()
{
InitializeComponent();
}
private void Threa1Start_Click(object sender, EventArgs e)
{
thread1.Start();
buttom();
textBox1.Text += "thread1.IsAlive:" + thread1.IsAlive + "/r/n";
}
private void Threa1Stop_Click(object sender, EventArgs e)
{
thread1.Abort();
thread1.Join();
FormShowStop1();
buttom();
textBox1.Text += "thread1.IsAlive:" + thread1.IsAlive + "/r/n";
}
private static void FormShow1()
{
MessageBox.Show("线程1启动");
}
private void FormShowStop1()
{
MessageBox.Show("线程1停止");
}
private void buttom()
{
if (thread1.ThreadState == ThreadState.Running)
{
Threa1Stop.Enabled = true;
Threa1Start.Enabled = false;
}
else
{
Threa1Stop.Enabled = false;
Threa1Start.Enabled = true;
}
}
}
}
以上代码初次运行没问题,但点击Threa1Stop停止后,再点击开始就没办了~~~问了一下google~
原来每次线程结束了,需要重新再new一次才可以正常进行~~~
于是改成:
private Thread thread1 = null;
再把
private void Threa1Start_Click(object sender, EventArgs e)
{
this.thread1 =new Thread(new ThreadStart(FormShow1));
thread1.Start();
buttom();
textBox1.Text += "thread1.IsAlive:" + thread1.IsAlive + "/r/n";
}
再运行,OK~~~
相关文章推荐
- 调用线程对象的start()方法会执行run(),为什么不能直接调用run()方法??
- 子线程为什么不能更新UI
- 为什么说invalidate()不能直接在线程中调用
- 线程对象不能多次调用start函数开辟线程
- 为什么DoDragDrop在线程中不能正常?请高手看看。
- 启动一个线程是用run()还是start()? 为什么?
- java再复习——多线程之初识线程,并从源码角度分析start与run方法,Thread类与Runnable接口
- 线程对象不能多次调用start函数开辟线程
- 为什么不能在子线程中更新UI
- java.lang.IllegalThreadStateException异常原因解析——同一个线程不能重复调用start方法
- 为什么在子线程中不能更新UI的简单解释
- 启动一个线程是用run()还是start()? 为什么?
- 广播接收器为什么不能开启线程?
- 为什么线程不能调用fork
- 求助~!线程里不能MediaRecorder.start()
- 为什么不能在子线程中更新UI
- 为什么说invalidate()不能直接在线程中调用
- 多线程:创建Thread为什么要调用start启动,而不能直接调用run方法
- 为什么调用thread.Abort(),线程不会马上停止
- GCD为什么不能在子线程更新UI