.NET Fiddle

CSharp · Console

/Program.cs

using System;
using System.Threading;
using System.Threading.Tasks;
					
public class Program
{
	public static async Task Main()
	{
		var cts = new CancellationTokenSource();

		var task = Task.Run(async () =>
		{
			try
			{
				await Task.Delay(TimeSpan.FromSeconds(30), cts.Token);
			}
			catch (TaskCanceledException) { }
			Thread.Sleep(TimeSpan.FromSeconds(3)); // output 1 2
			//await Task.Delay(TimeSpan.FromSeconds(3)); // outputs 2 1
			Console.WriteLine("1");
		});

		Thread.Sleep(TimeSpan.FromSeconds(3));

		cts.Cancel();
		Console.WriteLine("2");

		await task;
	}
}
;