AsyncBeginInvokePattern1

CSharp · Console

/Program.cs

using System;
using System.Threading;

public class Program
{
	public static void Main(string[] args)
	{
		Func<int> funcDelegate = DoWork;
		
		IAsyncResult asyncResult = funcDelegate.BeginInvoke(null, null);
		
		Console.WriteLine($"Main thread {Thread.CurrentThread.ManagedThreadId} does some work.");
		
		Thread.Sleep(TimeSpan.FromSeconds(2));
		
		int result = funcDelegate.EndInvoke(asyncResult);
		
		Console.WriteLine($"DoWork(30, 20) returned {result}");
	}

	static int DoWork()
	{
		Console.WriteLine($"Test method begins in thread {Thread.CurrentThread.ManagedThreadId}");
		
		Thread.Sleep(TimeSpan.FromSeconds(3));
		
		return 45;
	}
}
;