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