2009-05-15 Geoff Norton <gnorton@novell.com>
[mono-project.git] / mono / tests / thread6.cs
blobd1b6d81baff95692067048fcb5f93975f484634a
1 using System;
2 using System.Threading;
4 public class MultiThreadExceptionTest {
6 public static int result = 0;
7 public static object started = new object ();
9 public static void ThreadStart1 () {
10 Console.WriteLine("{0} started",
11 Thread.CurrentThread.Name);
13 try {
14 try {
15 try {
16 lock (started) {
17 Monitor.Pulse (started);
19 int i = 0;
20 try {
21 while (true) {
22 Console.WriteLine ("Count: " + i++);
23 Thread.Sleep (100);
26 catch (ThreadAbortException e) {
27 Console.WriteLine ("cought exception level 3 ");
29 // Check that the exception is only rethrown in
30 // the appropriate catch clauses
32 // This doesn't work currently, see
33 // http://bugzilla.ximian.com/show_bug.cgi?id=68552
36 try {
38 catch {}
39 try {
40 throw new DivideByZeroException ();
42 catch (Exception) {
45 result |= 32;
47 // Check that the exception is properly rethrown
49 result = 255;
50 } catch (ThreadAbortException e) {
51 Console.WriteLine ("cought exception level 2 " + e.ExceptionState);
52 Console.WriteLine (e);
53 if ((string)e.ExceptionState == "STATETEST")
54 result |= 1;
56 Thread.ResetAbort ();
57 throw e;
59 } catch (ThreadAbortException e) {
60 Console.WriteLine ("cought exception level 1 " + e.ExceptionState);
61 Console.WriteLine (e);
62 if (e.ExceptionState == null)
63 result |= 2;
65 } catch (Exception e) {
66 Console.WriteLine ("cought exception level 0")
67 ; Console.WriteLine (e);
68 Console.WriteLine (e.StackTrace);
69 result |= 4;
72 try {
73 Thread.ResetAbort ();
74 } catch (System.Threading.ThreadStateException e) {
75 result |= 8;
78 Console.WriteLine ("end");
79 result |= 16;
82 static string regress_78024 ()
84 try {
85 Thread.CurrentThread.Abort ();
86 } catch (Exception e) {
87 return "Got exception: " + e.Message;
88 } finally {
90 return "";
93 public static int Main() {
94 // Check aborting the current thread
95 bool aborted = false;
96 try {
97 Thread.CurrentThread.Abort ();
99 catch {
100 aborted = true;
101 Thread.ResetAbort ();
103 if (!aborted)
104 return 2;
106 Thread t1 = null;
108 lock (started) {
109 t1 = new Thread(new ThreadStart
110 (MultiThreadExceptionTest.ThreadStart1));
111 t1.Name = "Thread 1";
113 Thread.Sleep (100);
115 t1.Start();
117 Monitor.Wait (started);
120 Thread.Sleep (100);
122 t1.Abort ("STATETEST");
124 t1.Join ();
126 if (result != 59) {
127 Console.WriteLine ("Result: " + result);
128 return 1;
131 // Test from #68552
132 try {
133 try {
134 Run ();
135 } catch (Exception ex) {
138 return 2;
140 catch (ThreadAbortException ex) {
141 Thread.ResetAbort ();
144 // Test from #78024
145 try {
146 regress_78024 ();
147 return 3;
149 catch (ThreadAbortException ex) {
150 Thread.ResetAbort ();
153 return 0;
156 public static void Run ()
158 try {
159 Thread.CurrentThread.Abort ();
160 } catch (Exception ex) {
161 throw new Exception ("other");