michalsimon
4/2/2016 - 10:30 AM

Async Await Example

Async Await Example

using System;
using System.Threading;
using System.Threading.Tasks;

public static class Program
{
	public static void Main()
	{
		
	}

	private static async Task GetDataAsync()
	{
		var task1 = ReadDataFromIOAsync();
		var task2 = ReadDataFromIOAsync();
		// Here we can do more processing
		// that doesn't need the data from the previous calls.
		// Now we need the data so we have to wait
		await Task.WhenAll(task1, task2);
		// Now we have data to show.
		var t1 = task1.Result;
		var t2 = task2.Result;
	}

	public static Task<double> ReadDataFromIOAsync()
	{
		return Task.Run(new Func<double>(ReadDataFromIO));
	}

	public static double ReadDataFromIO()
	{
		// We are simulating an I/O by putting the current thread to sleep.
		Thread.Sleep(2000);
		return 10d;
	}
}