终止线程
private void Form1_Load(object sender, EventArgs e)
{
ThreadListener = new Thread(new ThreadStart(UdpListen));
ThreadListener.Start();//1
}
private void button2_Click(object sender, EventArgs e)
{
if (ThreadListener != null)
if (ThreadListener.IsAlive)
ThreadListener.Abort();//2
}
我按button2的时候,线程ThreadListener没有终止.当我再发送数据的时候,他提示我线程正在终止.然后才终止线程.
当我把//2放到//1的后面的时候.就能直接终止线程!这是怎么会事?
------解决方案--------------------终此线程本就有时间延迟.
------解决方案--------------------.Abort() will not terminate the thread immediately. Refer to MSDN.
If you want kill the thread ,try Win32 API TerminateThread(), but it is not recommend.