Have you ever encountered this exception? Why is this exception?
The following code snippet comes from msdn, Which is descriptive.
To put it simply, when the process still wants to execute, it finds that it has already called the abort method. since the thread itself has been suspended, it cannot be executed, so the exception is lost.
The following code is from msdn to explain the problem:
The following example demonstrates aborting a thread. The thread that corresponds es the threadabortexception uses the resetabort method to cancel the abort request and continue executing.
1: using System;
2: using System.Threading;
3: using System.Security.Permissions;
4:
5: public class ThreadWork {
6: public static void DoWork() {
7: try {
8: for(int i=0; i<100; i++) {
9: Console.WriteLine("Thread - working.");
10: Thread.Sleep(100);
11: }
12: }
13: catch(ThreadAbortException e) {
14: Console.WriteLine("Thread - caught ThreadAbortException - resetting.");
15: Console.WriteLine("Exception message: {0}", e.Message);
16: Thread.ResetAbort();
17: }
18: Console.WriteLine("Thread - still alive and working.");
19: Thread.Sleep(1000);
20: Console.WriteLine("Thread - finished working.");
21: }
22: }
23:
24: class ThreadAbortTest {
25: public static void Main() {
26: ThreadStart myThreadDelegate = new ThreadStart(ThreadWork.DoWork);
27: Thread myThread = new Thread(myThreadDelegate);
28: myThread.Start();
29: Thread.Sleep(100);
30: Console.WriteLine("Main - aborting my thread.");
31: myThread.Abort();
32: myThread.Join();
33: Console.WriteLine("Main ending.");
34: }
35: }
The output result is as follows:
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.