async task

CSharp · Console

/Program.cs

using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;


public class Demo
{
    static async Task foo()
    {
        await Task.Delay(100);
        throw new Exception("This exception is expected!"); 
    }
	
	static async void foo2()
    {
        await Task.Delay(100);
        throw new Exception("This exception is expected!"); 
    }
	
    public static void Main()
    {
      var task1 = new Task(foo2, CancellationToken.None, TaskCreationOptions.LongRunning);
      task1.Start();
	  //var task1 = Task.Run(foo);
      try
      {
          task1.Wait();
      }
      catch (AggregateException ae)
      {
          foreach (var e in ae.InnerExceptions) {
              if (e is Exception) {
                  Console.WriteLine("E: " + e.Message);
              }
              else {
                  throw;
              }
          }
      }
    }
}
;