您的位置:首页 > 其它

线程已被中止- “Thread was being aborted”

2013-05-07 14:13 513 查看

线程已被中止- “Thread was being aborted”

[code] using System;

using System.Threading;

using System.Security.Permissions;


public class ThreadWork {

 public static void DoWork() {

 try {

 for(int i=0; i<100; i++) {

 Console.WriteLine("Thread - working."); 

 Thread.Sleep(100);

}

}

 catch(ThreadAbortException e) {

 Console.WriteLine("Thread - caught ThreadAbortException - resetting.");

 Console.WriteLine("Exception message: {0}", e.Message);

 Thread.ResetAbort();

}

 Console.WriteLine("Thread - still alive and working."); 

 Thread.Sleep(1000);

 Console.WriteLine("Thread - finished working.");

}

}


class ThreadAbortTest {

 public static void Main() {

 ThreadStart myThreadDelegate = new ThreadStart(ThreadWork.DoWork);

 Thread myThread = new Thread(myThreadDelegate);

 myThread.Start();

 Thread.Sleep(100);

 Console.WriteLine("Main - aborting my thread.");

 myThread.Abort();

 myThread.Join();

 Console.WriteLine("Main ending."); 

}

}

[/code]
[/code]

下面是输出结果:


Thread - working.

Main - aborting my thread.

Thread - caught ThreadAbortException - resetting.

Exception message: Thread was being aborted.

Thread - still alive and working.

Thread - finished working.

Main ending.

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: