Merge pull request #495 from nicolas-raoul/fix-for-issue2907-with-no-formatting-changes
[mono.git] / mcs / tests / test-105.cs
1 using System;
2 using System.Threading;
3 using System.Runtime.InteropServices;
4
5 class Test {
6         delegate int SimpleDelegate (int a);
7
8         static int F (int a) {
9                 Console.WriteLine ("Test.F from delegate: " + a);
10                 Thread.Sleep (200);
11                 return a;
12         }
13
14         static void async_callback (IAsyncResult ar)
15         {
16                 Console.WriteLine ("Async Callback " + ar.AsyncState);
17         }
18         
19         public static int Main () {
20                 SimpleDelegate d = new SimpleDelegate (F);
21                 AsyncCallback ac = new AsyncCallback (async_callback);
22                 string state1 = "STATE1";
23                 string state2 = "STATE2";
24                 string state3 = "STATE3";
25                 string state4 = "STATE4";
26                 int fin = 0;
27                 
28                 IAsyncResult ar1 = d.BeginInvoke (1, ac, state1);
29                 IAsyncResult ar2 = d.BeginInvoke (2, ac, state2);
30                 IAsyncResult ar3 = d.BeginInvoke (3, ac, state3);
31                 IAsyncResult ar4 = d.BeginInvoke (4, ac, state4);
32                 
33                 int res = d.EndInvoke (ar1);
34
35                 Console.WriteLine ("Result = " + res);
36
37                 try {
38                         d.EndInvoke (ar1);
39                 } catch (InvalidOperationException) {
40                         Console.WriteLine ("cant execute EndInvoke twice ... OK");
41                 }
42
43                 ar1.AsyncWaitHandle.WaitOne ();
44                 if (ar1.IsCompleted) fin++;
45                 Console.WriteLine ("completed1: " + ar1.IsCompleted);
46                 ar2.AsyncWaitHandle.WaitOne ();
47                 if (ar2.IsCompleted) fin++;
48                 Console.WriteLine ("completed2: " + ar2.IsCompleted);
49                 ar3.AsyncWaitHandle.WaitOne ();
50                 if (ar3.IsCompleted) fin++;
51                 Console.WriteLine ("completed3: " + ar3.IsCompleted);
52                 ar4.AsyncWaitHandle.WaitOne ();         
53                 if (ar4.IsCompleted) fin++;
54                 Console.WriteLine ("completed4: " + ar4.IsCompleted);
55
56                 if (fin != 4)
57                         return 1;
58                 
59                 return 0;
60         }
61 }