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