2 using System
.Collections
.Generic
;
3 using System
.Reflection
;
4 using System
.Threading
;
5 using System
.Runtime
.CompilerServices
;
10 static bool finally_done
;
11 static ManualResetEvent handle
;
15 [MethodImplAttribute (MethodImplOptions
.NoInlining
)]
16 static void ThrowE () {
20 static bool InterruptRequested () {
21 return (Thread
.CurrentThread
.ThreadState
& ThreadState
.AbortRequested
) == ThreadState
.AbortRequested
;
24 [MethodImplAttribute (MethodImplOptions
.NoInlining
)]
25 public static void SleepLoop () {
26 for (int i
= 0; i
< 10; ++i
) {
27 Console
.WriteLine ("step {0} - {1}", i
, Thread
.CurrentThread
.ThreadState
);
28 if (InterruptRequested ())
33 if (!InterruptRequested ())
38 } catch (Exception e
) {
39 Console
.WriteLine ("caught/0 {0} from inside the prot block", e
.GetType ());
40 if (!(e
is NullReferenceException
))
45 [MethodImplAttribute (MethodImplOptions
.NoInlining
)]
46 public static void CancelAbort () {
47 object lk
= new object ();
48 Console
.WriteLine ("step 0 - {0}", Thread
.CurrentThread
.ThreadState
);
49 //lock (lk) { Monitor.Wait (lk, 100); }
50 Console
.WriteLine ("step 1 - {0}", Thread
.CurrentThread
.ThreadState
);
54 /////////////////////////////////////////////////////
55 [MethodImplAttribute (MethodImplOptions
.NoInlining
)]
56 static void InnerFromEH0 () {
57 thread
= Thread
.CurrentThread
;
58 MethodInfo mi
= typeof (Driver
).GetMethod ("SleepLoop");
61 throw new ArgumentException ();
65 Console
.WriteLine ("done");
68 Console
.WriteLine ("After finally");
70 } catch (Exception e
) {
71 if (!(e
is ArgumentException
))
73 Console
.WriteLine ("caught/1 a {0} while on {1} res {2}", e
.GetType (), Thread
.CurrentThread
.ThreadState
, result
);
77 [MethodImplAttribute (MethodImplOptions
.NoInlining
)]
78 static void GuardFromEH0 () {
81 } catch (Exception e
) {
82 if (!(e
is ThreadAbortException
))
84 Console
.WriteLine ("caught/2 a {0} while on {1} res {2}", e
.GetType (), Thread
.CurrentThread
.ThreadState
, result
);
89 public static int test_0_abort_finally_after_throw () {
92 Action ac
= GuardFromEH0
;
93 handle
= new ManualResetEvent (false);
94 var res
= ac
.BeginInvoke (null, null);
96 Console
.WriteLine ("aborting");
98 Console
.WriteLine ("aborted");
99 res
.AsyncWaitHandle
.WaitOne ();
100 Console
.WriteLine ("waited");
106 /////////////////////////////////////////////////////
108 [MethodImplAttribute (MethodImplOptions
.NoInlining
)]
109 static void InnerFromEH1 () {
110 thread
= Thread
.CurrentThread
;
111 MethodInfo mi
= typeof (Driver
).GetMethod ("SleepLoop");
114 throw new ArgumentException ();
119 Console
.WriteLine ("done");
122 Console
.WriteLine ("After finally");
124 } catch (Exception e
) {
125 if (!(e
is ArgumentException
))
127 Console
.WriteLine ("caught/3 a {0} while on {1} res {2}", e
.GetType (), Thread
.CurrentThread
.ThreadState
, result
);
131 [MethodImplAttribute (MethodImplOptions
.NoInlining
)]
132 static void GuardFromEH1 () {
135 } catch (Exception e
) {
137 Console
.WriteLine ("caught/4 a {0} while on {1}", e
.GetType (), Thread
.CurrentThread
.ThreadState
);
141 public static int test_0_abort_finally_and_cancel () {
142 finally_done
= false;
144 Action ac
= GuardFromEH1
;
145 handle
= new ManualResetEvent (false);
146 var res
= ac
.BeginInvoke (null, null);
148 Console
.WriteLine ("aborting");
150 Console
.WriteLine ("aborted");
151 res
.AsyncWaitHandle
.WaitOne ();
152 Console
.WriteLine ("waited");
158 /////////////////////////////////////////////////////
160 [MethodImplAttribute (MethodImplOptions
.NoInlining
)]
161 static void InnerFromEH () {
162 thread
= Thread
.CurrentThread
;
163 MethodInfo mi
= typeof (Driver
).GetMethod ("SleepLoop");
166 Console
.WriteLine ("try block");
170 Console
.WriteLine ("done");
173 Console
.WriteLine ("After finally");
175 } catch (Exception e
) {
176 if (!(e
is ThreadAbortException
))
178 Console
.WriteLine ("caught/5 a {0} while on {1} res {2}", e
.GetType (), Thread
.CurrentThread
.ThreadState
, result
);
182 [MethodImplAttribute (MethodImplOptions
.NoInlining
)]
183 static void GuardFromEH () {
186 } catch (Exception e
) {
187 if (!(e
is ThreadAbortException
))
189 Console
.WriteLine ("caught/6 a {0} while on {1} res {2}", e
.GetType (), Thread
.CurrentThread
.ThreadState
, result
);
194 public static int test_0_finally_after_try () {
195 AppDomain
.CurrentDomain
.UnhandledException
+= (obj
, sender
) => {
196 Console
.WriteLine ("Unhandled {0}", sender
.ExceptionObject
);
199 finally_done
= false;
201 Action ac
= GuardFromEH
;
202 handle
= new ManualResetEvent (false);
203 var res
= ac
.BeginInvoke (null, null);
205 Console
.WriteLine ("aborting");
207 Console
.WriteLine ("aborted");
208 res
.AsyncWaitHandle
.WaitOne ();
209 Console
.WriteLine ("waited");
214 /////////////////////////////////////////////////////
216 static int Main (string[] args
)
218 AppDomain
.CurrentDomain
.UnhandledException
+= (obj
, sender
) => {
219 Console
.WriteLine ("Unhandled {0}", sender
.ExceptionObject
);
222 return TestDriver
.RunTests (typeof (Driver
), args
);