2007-03-28 Chris Toshok <toshok@ximian.com>
[mono-project.git] / mono / tests / thread6.cs
blob0d25eff412d10083807a782c406a67056b62fb27
1 using System;
2 using System.Threading;
4 public class MultiThreadExceptionTest {
6 public static int result = 0;
8 public static void ThreadStart1 () {
9 Console.WriteLine("{0} started",
10 Thread.CurrentThread.Name);
12 try {
13 try {
14 try {
15 int i = 0;
16 try {
17 while (true) {
18 Console.WriteLine ("Count: " + i++);
19 Thread.Sleep (100);
22 catch (ThreadAbortException e) {
23 Console.WriteLine ("cought exception level 3 ");
25 // Check that the exception is only rethrown in
26 // the appropriate catch clauses
28 // This doesn't work currently, see
29 // http://bugzilla.ximian.com/show_bug.cgi?id=68552
32 try {
34 catch {}
35 try {
36 throw new DivideByZeroException ();
38 catch (Exception) {
41 result |= 32;
43 // Check that the exception is properly rethrown
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;
52 Thread.ResetAbort ();
53 throw e;
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;
61 } catch (Exception e) {
62 Console.WriteLine ("cought exception level 0")
63 ; Console.WriteLine (e);
64 Console.WriteLine (e.StackTrace);
65 result |= 4;
68 try {
69 Thread.ResetAbort ();
70 } catch (System.Threading.ThreadStateException e) {
71 result |= 8;
74 Console.WriteLine ("end");
75 result |= 16;
78 static string regress_78024 ()
80 try {
81 Thread.CurrentThread.Abort ();
82 } catch (Exception e) {
83 return "Got exception: " + e.Message;
84 } finally {
86 return "";
89 public static int Main() {
91 // Check aborting the current thread
92 bool aborted = false;
93 try {
94 Thread.CurrentThread.Abort ();
96 catch {
97 aborted = true;
98 Thread.ResetAbort ();
100 if (!aborted)
101 return 2;
103 Thread t1 = new Thread(new ThreadStart
104 (MultiThreadExceptionTest.ThreadStart1));
105 t1.Name = "Thread 1";
107 Thread.Sleep (100);
109 t1.Start();
111 Thread.Sleep (200);
112 t1.Abort ("STATETEST");
114 t1.Join ();
115 Console.WriteLine ("Result: " + result);
117 if (result != 59)
118 return 1;
120 // Test from #68552
121 try {
122 try {
123 Run ();
124 } catch (Exception ex) {
127 return 2;
129 catch (ThreadAbortException ex) {
130 Thread.ResetAbort ();
133 // Test from #78024
134 try {
135 regress_78024 ();
136 return 3;
138 catch (ThreadAbortException ex) {
139 Thread.ResetAbort ();
142 return 0;
145 public static void Run ()
147 try {
148 Thread.CurrentThread.Abort ();
149 } catch (Exception ex) {
150 throw new Exception ("other");