首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

c#中 线程有关问题 IsAlive的有关问题

2012-01-01 
c#中 线程问题 IsAlive的问题private void button1_Click(object sender, System.EventArgs e){tnew Thre

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后,很快就执行结束了

热点排行