Merge pull request #4938 from kumpera/optimize_ref_queries
[mono.git] / mono / tests / async-with-cb-throws.cs
1 using System;
2 using System.Threading;
3 using System.Runtime.InteropServices;
4
5 class AsyncException : Exception {}
6
7 class Tests
8 {
9         delegate int SimpleDelegate (int a);
10
11         static int cb_state = 0;
12
13         static int async_func (int a)
14         {
15                 Console.WriteLine ("async_func from delegate: " + a);
16                 return 10;
17         }
18
19         static int async_func_throws (int a)
20         {
21                 Console.WriteLine ("async_func_throws from delegate: " + a);
22                 throw new AsyncException ();
23         }
24
25         static void async_callback (IAsyncResult ar)
26         {
27                 Console.WriteLine ("Async Callback " + ar.AsyncState);
28                 cb_state = 1;
29         }
30
31         static int Main ()
32         {
33                 SimpleDelegate d = new SimpleDelegate (async_func_throws);
34                 AsyncCallback ac = new AsyncCallback (async_callback);
35                 string state1 = "STATE1";
36
37                 // Call delegate via ThreadPool and check that the exception is rethrown correctly
38                 IAsyncResult ar1 = d.BeginInvoke (1, ac, state1);
39
40                 while (cb_state == 0)
41                         Thread.Sleep (0);
42
43                 try {
44                         d.EndInvoke (ar1);
45                         Console.WriteLine ("NO EXCEPTION");
46                         return 1;
47                 } catch (AsyncException) {
48                         Console.WriteLine ("received exception ... OK");
49                         return 0;
50                 } catch (Exception e) {
51                         Console.WriteLine ("wrong exception {0}", e);
52                         return 3;
53                 }
54         }
55 }