dotnet / dotnet/command-line-api
Problem running async Actions
- Dominant language
- C#
- Stars
- 3.7k
- Forks
- 428
- PR merge metrics
- No merged PRs in 30d
Description
While porting an existing project from the beta4 package in nuget to a more recent build (beta4.23181.5) I saw that async actions were not running to completion. After some debugging I found that if I made the action non-async, it would work as expected. I was able to reproduce it with the following.
```c#
using System;
using System.CommandLine;
using System.Threading.Tasks;
namespace CliAsyncTest;
public class Program
{
static async Task Main(string[] args)
{
var rootCmd = new CliRootCommand("CliAsyncTest - Test InvokeAsync()");
rootCmd.SetAction(async (_) => // Fix: remove async
{
Console.WriteLine("rootCmd Action starting");
await Task.Delay(100); // Fix: remove await, add .Wait()
Console.WriteLine("rootCmd Action complete");
});
return await rootCmd.Parse(args).InvokeAsync();
}
}
```
When I run this I will see the first line output to the console, but not the second. After applying the fixes in the comments I will see both lines output.
Tested with:
- beta4.23181.5
- Windows 10
Contributor guide
Assessment
This issue has not been assessed yet.