日期:2014-05-18  浏览次数:21441 次

c#中 线程问题 IsAlive的问题
private void button1_Click(object sender, System.EventArgs e) 
{ 
t=new Thread(new ThreadStart(this.threadRead)); 
button2.Enabled = true; 
t.Start(); 
} 
private void threadRead() 
{ 
this.SetText("this is a new Thread"); 
} 
delegate void setListBox(string text); 
public void SetText(string text) 
{ 
// InvokeRequired required compares the thread ID of the 
// calling thread to the thread ID of the creating thread. 
// If these threads are different, it returns true. 
if (this.listBox1.InvokeRequired) 
{ 
setListBox d = new setListBox(SetText); 
this.Invoke(d, new object[] { text }); 
} 
else 
{ 

this.listBox1.Items.Add (text); 
button1.Enabled = false; 

} 
} 
private void button2_Click(object sender, System.EventArgs e) 
{ 
if (t.IsAlive) 
{ 
t.Abort(); 
this.listBox1.Items.Add("the thread is stop!!!"); 

} 

else 
{ 
this.listBox1.Items.Add("the thread is not started"); 
button2.Enabled = false; 

} 
button1.Enabled = true; 
} 
这段代码很简单 启动进程我 实习了 但是在 我点击button2的时候 t.IsAlive 在还没运行到下面 这段语句的时候 就 自己变成 false了。 
这是怎么回事呀 
if (t.IsAlive) 
{ 
t.Abort(); 
this.listBox1.Items.Add("the thread is stop!!!"); 

}
小弟新手 以前没用过线程的问题 谢谢大家帮助

------解决方案--------------------
蛮诡异的,单看代码没道理就停了呀。。。
------解决方案--------------------

因为线程start后,很快就执行结束了