2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mono / tests / thread6.cs
1 using System;
2 using System.Threading;
3
4 public class MultiThreadExceptionTest {
5
6         public static int result = 0;
7         
8         public static void ThreadStart1 () {
9                 Console.WriteLine("{0} started", 
10                                   Thread.CurrentThread.Name);
11
12                 try {
13                         try {
14                                 try {
15                                         int i = 0;
16                                         try {
17                                                 while (true) {
18                                                         Console.WriteLine ("Count: " + i++);
19                                                         Thread.Sleep (100);
20                                                 }
21                                         }
22                                         catch (ThreadAbortException e) {
23                                                 Console.WriteLine ("cought exception level 3 ");
24
25                                                 // Check that the exception is only rethrown in
26                                                 // the appropriate catch clauses
27
28                                                 try {
29                                                 }
30                                                 catch {}
31                                                 try {
32                                                         throw new DivideByZeroException ();
33                                                 }
34                                                 catch (Exception) {
35                                                 }
36                                                 result |= 32;
37
38                                                 // Check that the exception is properly rethrown
39                                         }
40                                         result = 255;
41                                 } catch (ThreadAbortException e) {
42                                         Console.WriteLine ("cought exception level 2 " + e.ExceptionState);
43                                         Console.WriteLine (e);
44                                         if ((string)e.ExceptionState == "STATETEST")
45                                                 result |= 1;
46
47                                         Thread.ResetAbort ();
48                                         throw e;
49                                 }
50                         } catch (ThreadAbortException e) {
51                                 Console.WriteLine ("cought exception level 1 " + e.ExceptionState);
52                                 Console.WriteLine (e);
53                                 if (e.ExceptionState == null)
54                                         result |= 2;
55                         }
56                 } catch (Exception e) {
57                         Console.WriteLine ("cought exception level 0")
58 ;                       Console.WriteLine (e);
59                         Console.WriteLine (e.StackTrace);
60                         result |= 4;
61                 }
62
63                 try {
64                         Thread.ResetAbort ();
65                 } catch (System.Threading.ThreadStateException e) {
66                         result |= 8;                    
67                 }
68                 
69                 Console.WriteLine ("end");
70                 result |= 16;
71         }
72         
73         public static int Main() {
74
75                 // Check aborting the current thread
76                 bool aborted = false;
77                 try {
78                         Thread.CurrentThread.Abort ();
79                 }
80                 catch {
81                         aborted = true;
82                         Thread.ResetAbort ();
83                 }
84                 if (!aborted)
85                         return 2;
86
87                 Thread t1 = new Thread(new ThreadStart
88                         (MultiThreadExceptionTest.ThreadStart1));
89                 t1.Name = "Thread 1";
90
91                 Thread.Sleep (100);
92                 
93                 t1.Start();
94                 
95                 Thread.Sleep (200);
96                 t1.Abort ("STATETEST");
97
98                 t1.Join ();
99                 Console.WriteLine ("Result: " + result);
100
101                 if (result != 59)
102                         return 1;
103
104                 return 0;
105         }
106 }
107