29
1
using System;
2
using System.Threading;
3
4
public class Program
5
{
6
public static void Main(string[] args)
7
{
8
Func<int> funcDelegate = DoWork;
9
10
IAsyncResult asyncResult = funcDelegate.BeginInvoke(null, null);
11
12
Console.WriteLine($"Main thread {Thread.CurrentThread.ManagedThreadId} does some work.");
13
14
Thread.Sleep(TimeSpan.FromSeconds(2));
15
16
int result = funcDelegate.EndInvoke(asyncResult);
17
18
Console.WriteLine($"DoWork(30, 20) returned {result}");
19
}
20
21
static int DoWork()
22
{
23
Console.WriteLine($"Test method begins in thread {Thread.CurrentThread.ManagedThreadId}");
24
25
Thread.Sleep(TimeSpan.FromSeconds(3));
26
27
return 45;
28
}
29
}
Cached Result
Main thread 125 does some work.
Test method begins in thread 110
DoWork(30, 20) returned 45
Test method begins in thread 110
DoWork(30, 20) returned 45