* roottypes.cs: Rename from tree.cs.
[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                                                 // This doesn't work currently, see
29                                                 // http://bugzilla.ximian.com/show_bug.cgi?id=68552
30
31                                                 /*
32                                                 try {
33                                                 }
34                                                 catch {}
35                                                 try {
36                                                         throw new DivideByZeroException ();
37                                                 }
38                                                 catch (Exception) {
39                                                 }
40                                                 */
41                                                 result |= 32;
42
43                                                 // Check that the exception is properly rethrown
44                                         }
45                                         result = 255;
46                                 } catch (ThreadAbortException e) {
47                                         Console.WriteLine ("cought exception level 2 " + e.ExceptionState);
48                                         Console.WriteLine (e);
49                                         if ((string)e.ExceptionState == "STATETEST")
50                                                 result |= 1;
51
52                                         Thread.ResetAbort ();
53                                         throw e;
54                                 }
55                         } catch (ThreadAbortException e) {
56                                 Console.WriteLine ("cought exception level 1 " + e.ExceptionState);
57                                 Console.WriteLine (e);
58                                 if (e.ExceptionState == null)
59                                         result |= 2;
60                         }
61                 } catch (Exception e) {
62                         Console.WriteLine ("cought exception level 0")
63 ;                       Console.WriteLine (e);
64                         Console.WriteLine (e.StackTrace);
65                         result |= 4;
66                 }
67
68                 try {
69                         Thread.ResetAbort ();
70                 } catch (System.Threading.ThreadStateException e) {
71                         result |= 8;                    
72                 }
73                 
74                 Console.WriteLine ("end");
75                 result |= 16;
76         }
77
78         static string regress_78024 ()
79         {
80                 try {
81                         Thread.CurrentThread.Abort ();
82                 } catch (Exception e) {
83                         return "Got exception: " + e.Message;
84                 } finally {
85                 }
86                 return "";
87         }
88         
89         public static int Main() {
90
91                 // Check aborting the current thread
92                 bool aborted = false;
93                 try {
94                         Thread.CurrentThread.Abort ();
95                 }
96                 catch {
97                         aborted = true;
98                         Thread.ResetAbort ();
99                 }
100                 if (!aborted)
101                         return 2;
102
103                 Thread t1 = new Thread(new ThreadStart
104                         (MultiThreadExceptionTest.ThreadStart1));
105                 t1.Name = "Thread 1";
106
107                 Thread.Sleep (100);
108                 
109                 t1.Start();
110                 
111                 Thread.Sleep (200);
112                 t1.Abort ("STATETEST");
113
114                 t1.Join ();
115                 Console.WriteLine ("Result: " + result);
116
117                 if (result != 59)
118                         return 1;
119
120                 // Test from #68552
121                 try {
122                         try {
123                                 Run ();
124                         } catch (Exception ex) {
125                         }
126
127                         return 2;
128                 }
129                 catch (ThreadAbortException ex) {
130                         Thread.ResetAbort ();
131                 }
132
133                 // Test from #78024
134                 try {
135                         regress_78024 ();
136                         return 3;
137                 }
138                 catch (ThreadAbortException ex) {
139                         Thread.ResetAbort ();
140                 }
141
142                 return 0;
143         }
144
145         public static void Run ()
146         {
147                 try {
148                         Thread.CurrentThread.Abort ();
149                 } catch (Exception ex) {
150                         throw new Exception ("other");
151                 }
152         }
153 }
154