32
1
using System;
2
using System.Threading;
3
4
public class Program
5
{
6
public static void Main()
7
{
8
Func<int, int, int> funcDelegate = (int num1, int num2) =>
9
{
10
Console.WriteLine($"Test method begins in thread {Thread.CurrentThread.ManagedThreadId}");
11
12
Thread.Sleep(TimeSpan.FromSeconds(3));
13
14
return num1 + num2;
15
};
16
17
IAsyncResult asyncResult = funcDelegate.BeginInvoke(30, 20, null, null);
18
19
Console.WriteLine($"Main thread {Thread.CurrentThread.ManagedThreadId} does some work.\n");
20
21
while (!asyncResult.IsCompleted)
22
{
23
Thread.Sleep(250);
24
Console.Write(".");
25
}
26
27
//This wil return result without delay. Because, WaitOne() lets finishing child operation.
28
int result = funcDelegate.EndInvoke(asyncResult);
29
30
Console.WriteLine($"\nDoWork(30, 20) returned {result}");
31
}
32
}
Cached Result
2
2 4
2 4 6
2 4 6 8
2 4 6 8 10
2 4 6 8 10 12
2 4 6 8 10 12 14
2 4 6 8 10 12 14 16
2 4 6 8 10 12 14 16 18
2 4 6 8 10 12 14 16
2 4 6 8 10 12 14
2 4 6 8 10 12
2 4 6 8 10
2 4 6 8
2 4 6
2 4
2
2 4
2 4 6
2 4 6 8
2 4 6 8 10
2 4 6 8 10 12
2 4 6 8 10 12 14
2 4 6 8 10 12 14 16
2 4 6 8 10 12 14 16 18
2 4 6 8 10 12 14 16
2 4 6 8 10 12 14
2 4 6 8 10 12
2 4 6 8 10
2 4 6 8
2 4 6
2 4
2