2 using System.Diagnostics;
3 using System.Threading;
4 using System.Threading.Tasks;
6 class CustomException : Exception
10 class CustomException2 : Exception
15 class CrossDomain : MarshalByRefObject
17 public Action NewDelegateWithTarget ()
19 return new Action (Bar);
22 public Action NewDelegateWithoutTarget ()
24 return () => { throw new CustomException (); };
29 throw new CustomException ();
34 static ManualResetEvent mre = new ManualResetEvent (false);
36 static void DoTest1 ()
40 var t = new Thread (new ThreadStart (() => { try { throw new CustomException (); } finally { mre.Set (); } }));
43 if (!mre.WaitOne (5000))
49 static void DoTest2 ()
53 var a = new Action (() => { try { throw new CustomException (); } finally { mre.Set (); } });
54 var ares = a.BeginInvoke (null, null);
56 if (!mre.WaitOne (5000))
61 throw new Exception ();
62 } catch (CustomException) {
68 static void DoTest3 ()
72 ThreadPool.QueueUserWorkItem (_ => { try { throw new CustomException (); } finally { mre.Set (); } });
74 if (!mre.WaitOne (5000))
78 static void DoTest4 ()
82 var t = Task.Factory.StartNew (new Action (() => { try { throw new CustomException (); } finally { mre.Set (); } }));
84 if (!mre.WaitOne (5000))
89 throw new Exception ();
90 } catch (AggregateException ae) {
91 if (!(ae.InnerExceptions [0] is CustomException))
103 throw new CustomException ();
110 static void DoTest5 ()
114 new FinalizedClass();
117 GC.WaitForPendingFinalizers ();
119 if (!mre.WaitOne (5000))
120 Environment.Exit (2);
123 static void DoTest6 ()
125 ManualResetEvent mre2 = new ManualResetEvent (false);
129 var a = new Action (() => { try { throw new CustomException (); } finally { mre.Set (); } });
130 var ares = a.BeginInvoke (_ => { mre2.Set (); throw new CustomException2 (); }, null);
132 if (!mre.WaitOne (5000))
133 Environment.Exit (2);
134 if (!mre2.WaitOne (5000))
135 Environment.Exit (22);
139 throw new Exception ();
140 } catch (CustomException) {
141 } catch (Exception) {
142 Environment.Exit (3);
146 static void DoTest7 ()
148 var cd = (CrossDomain) AppDomain.CreateDomain ("ad").CreateInstanceAndUnwrap (typeof(CrossDomain).Assembly.FullName, "CrossDomain");
150 var a = cd.NewDelegateWithoutTarget ();
151 var ares = a.BeginInvoke (delegate { throw new CustomException2 (); }, null);
155 throw new Exception ();
156 } catch (CustomException) {
157 } catch (Exception) {
158 Environment.Exit (3);
162 static void DoTest8 ()
164 var cd = (CrossDomain) AppDomain.CreateDomain ("ad").CreateInstanceAndUnwrap (typeof(CrossDomain).Assembly.FullName, "CrossDomain");
166 var a = cd.NewDelegateWithTarget ();
167 var ares = a.BeginInvoke (delegate { throw new CustomException2 (); }, null);
171 throw new Exception ();
172 } catch (CustomException) {
173 } catch (Exception) {
174 Environment.Exit (3);
178 static void Main (string[] args)
180 switch (int.Parse (args [0])) {
181 case 1: DoTest1 (); break;
182 case 2: DoTest2 (); break;
183 case 3: DoTest3 (); break;
184 case 4: DoTest4 (); break;
185 case 5: DoTest5 (); break;
186 case 6: DoTest6 (); break;
187 case 7: DoTest7 (); break;
188 case 8: DoTest8 (); break;
189 default: throw new ArgumentOutOfRangeException ();
191 Environment.Exit (0);