
Image retained from the original article.
Originally published 10 October 2023; rewritten and reviewed on 15 October 2026.
Almost every C# console utility starts out synchronous. You write a quick script to sync orders, push files, or call a webhook, and then one day you swap WebClient for HttpClient or upgrade an SDK whose methods are all async Task. Suddenly static void Main(string[] args) won’t let you use await.
If you search for a quick fix, you’ll immediately see .GetAwaiter().GetResult() or .Result everywhere. It works in a console app, which is why it shows up in so many snippets, but it’s worth understanding when to avoid it and how async Task Main replaces it cleanly.
Use async Task Main whenever your compiler supports it
Since C# 7.1 (which covers .NET Core 2.0+, .NET 5/6/8+, and any modern .NET Framework 4.8 project with <LangVersion>latest</LangVersion> in its .csproj), the compiler natively supports asynchronous entry points: static async Task Main(string[] args) and static async Task<int> Main(string[] args).
Under the hood, the compiler still generates a synchronous Main wrapper that calls .GetAwaiter().GetResult() on your behalf, so the CLR entry-point contract doesn’t change. The difference is readability and error handling—your own code stays purely async/await from top to bottom, and you can return an integer exit code for cron jobs or CI pipelines:
using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
internal static class Program
{
private static readonly HttpClient Http = new()
{
Timeout = TimeSpan.FromSeconds(15)
};
private static async Task<int> Main(string[] args)
{
using var cts = new CancellationTokenSource();
Console.CancelKeyPress += (_, e) =>
{
e.Cancel = true;
cts.Cancel();
};
try
{
string url = args.Length > 0 ? args[0] : "https://httpbin.org/get";
using HttpResponseMessage response = await Http.GetAsync(url, cts.Token);
response.EnsureSuccessStatusCode();
string body = await response.Content.ReadAsStringAsync(cts.Token);
Console.WriteLine($"Received {body.Length} characters.");
return 0;
}
catch (OperationCanceledException)
{
Console.Error.WriteLine("Cancelled by user.");
return 2;
}
catch (Exception ex)
{
Console.Error.WriteLine($"Request failed: {ex.Message}");
return 1;
}
}
}
Returning 0 on success and non-zero on failure matters if this console app runs as a scheduled task on Windows or a systemd timer on Linux. Otherwise, a crashing job can silently look like it succeeded to whatever orchestrator launched it.
Why .GetAwaiter().GetResult() beats .Result—and where both go wrong
If you are stuck on an older C# 7.0 or earlier project where you cannot change <LangVersion> or the Main signature, you have to block at the boundary:
private static int Main(string[] args)
{
try
{
return RunAsync(args).GetAwaiter().GetResult();
}
catch (Exception ex)
{
Console.Error.WriteLine(ex.Message);
return 1;
}
}
Developers prefer .GetAwaiter().GetResult() over .Result or .Wait() for one specific reason: exception unwrapping. .Result wraps any thrown exception inside an AggregateException, forcing you to dig into ex.InnerException to see the real HttpRequestException or SqlException. GetAwaiter().GetResult() throws the original exception directly with its stack trace intact.
That said, do not copy this pattern out of Program.cs into an ASP.NET MVC controller, WinForms button click, or WPF command. Console apps do not have a SynchronizationContext, so blocking the main thread while an await resumes on a thread-pool thread doesn’t deadlock. In classic ASP.NET or desktop UI apps, a single-threaded SynchronizationContext is present, and calling .GetAwaiter().GetResult() on that thread will freeze the request or the UI permanently because the continuation is waiting for the very thread you just blocked.
Never fire-and-forget in Main
One last trap I occasionally see in legacy scripts is dropping await and hoping the background task finishes:
// Broken: the process exits before SendEmailAsync completes
_ = SendEmailAsync();
In a console app, the process terminates the moment Main returns. Background Task threads are thread-pool threads (marked as background threads by the CLR), so the runtime will kill them mid-flight without waiting. Always await every piece of work your CLI tool is responsible for, and test both a happy-path run and a forced failure (such as a bad URL or an unreachable database) so you know the process waits, logs clearly, and exits with the right code.