warning CS4014: Because this call is not awaited, execution of the current method continues before the call is completed.
You called an async method and did not wait for it. The build succeeds, but the next lines
run before the work is done, and a short program can end before it happens at all. Put
await in front of the call.
Treat this warning as a bug report. In the program below it meant an order file that was never written, with no error anywhere. Unlike CS0029, nothing here fails to compile, which is how it slips through: the build log shows one warning and a success line, and both are easy to scroll past.
The error
...\Program.cs(4,1): warning CS4014: Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call. [...\FixLab.csproj]
Build succeeded.
The folder part of both paths was shortened to ...; nothing else was changed.
Why it happens
string path = "order-1001.txt";
File.Delete(path);
SaveOrderAsync(path, "Order 1001 for David Chen");
Console.WriteLine($"Saved: {File.Exists(path)}");
static async Task SaveOrderAsync(string path, string text)
{
await Task.Delay(500); // stands in for a slow disk or network call
await File.WriteAllTextAsync(path, text);
Console.WriteLine("Write finished");
}
SaveOrderAsync runs until its first unfinished await and then
returns a Task to the caller. Nobody waits on that task, so the next line runs
at once. The run showed exactly that:
PS> dotnet run --no-build
Saved: False
exit code: 0
PS> Test-Path order-1001.txt (1.5 s after the program ended)
False
Write finished never appeared and the file was never created: the program
reached the end of its statements and exited while the save was still waiting. The exit code
was 0, so nothing told the user anything had gone wrong.
The fix
string path = "order-1001.txt";
File.Delete(path);
await SaveOrderAsync(path, "Order 1001 for David Chen");
Console.WriteLine($"Saved: {File.Exists(path)}");
static async Task SaveOrderAsync(string path, string text)
{
await Task.Delay(500); // stands in for a slow disk or network call
await File.WriteAllTextAsync(path, text);
Console.WriteLine("Write finished");
}
One word. The project built with warnings treated as errors, and the run printed
Write finished and then Saved: True. If you really mean to start
work and not wait for it, keep the task in a variable and await it later; discarding it
with _ = is not a fix: in a side test it built with 0 warnings and still
printed Saved: False and wrote no file.
How it was reproduced
A dotnet new console project on .NET SDK 10.0.403 (net10.0, nullable and
implicit usings on, no packages). dotnet build --no-incremental succeeded with
the CS4014 warning, the run printed Saved: False and the file did not exist
afterwards; the fixed version passed
dotnet build --no-incremental -p:TreatWarningsAsErrors=true, and its run
created the file.
Frequently asked
- How do I fix warning CS4014 in C#?
- Put await in front of the async call, and make the calling method async Task if it is not already. Then the next line runs only after the async work has finished.
- Is it safe to ignore CS4014?
- Rarely. In the test program the un-awaited save never wrote its file, the program exited with code 0 and printed no error. Ignore it only when you deliberately start background work and handle its result elsewhere.
- Does _ = MyMethodAsync() fix CS4014?
- It removes the warning by discarding the task, but the work is still not awaited: the method continues at once and the program can still exit before the work finishes.
The same missing await becomes a hard error when the result is assigned; see
CS0029:
cannot convert Task<int> to int. More are in the
Fixes category.