VisualBean
3/9/2018 - 10:15 AM

before and after async

before and after async

public static void MyTask()
{
  	var task = Task.Run(() =>
	{
		Thread.Sleep(2000);
	});
  	//What to do when task has finished.
  	task.ContinueWith((s) =>
	{
	  	//If this was a forms app we would have to do Dispatcher.Invoke to actually call something on the UI thread.
	  	Console.WriteLine("Task has finished");
	});

  //anything here will probably run before the tasks has finished.
}

public static async Task MyTaskAsync()
{
  	await Task.Run(() =>
 	{
   		Thread.Sleep(2000);
	});
  	//Since we awaited - this will run once the above is done.
  	Console.WriteLine("the async task has finished");

  	//Anything here will run after the above has finished -- Continuation.
}