Execute Async

CSharp · Console

/Program.cs

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

namespace Example
{
	public class Program
	{
		static void Log(string message)
		{
		    Console.WriteLine
				(
				    "{0:yyyy-MM-dd HH:mm:ss.fff} [{1:X4}] {2}", 
				    DateTime.Now, 
				    Thread.CurrentThread.ManagedThreadId, 
				    message
			    );
		}
		
		static async Task Execute(params Func<Task>[] actions)
		{
			var tasks = actions.Select( action => action() );
			await Task.WhenAll(tasks);
		}
	
		public static async Task MainAsync()
		{
			await Execute
			(
				async () => 
				{
					await Task.Delay(3000);
					Log("Function 1 done");
				}
				,
				async () =>
				{
					await Task.Delay(3000);
					Log("Function 2 done");
				}
			);
				
		}
		
		public static void Main()
		{
			Log("Run started.");
			MainAsync().GetAwaiter().GetResult();
			Log("Run complete.");
		}
	}
}
;