linux-packaging-mono/mono/tests/async-with-cb-throws.cs
Jo Shields a575963da9 Imported Upstream version 3.6.0
Former-commit-id: da6be194a6b1221998fc28233f2503bd61dd9d14
2014-08-13 10:39:27 +01:00

56 lines
1.2 KiB
C#

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;
}
}
}