How to use this library with async actions
Hi there,
great package, it gets you started real quick. I try to use it with some simple async code and Serilog logging. But as soon as I have executed the first menu action the Serilog logging won't work anymore.
It seem like the use of GetAwaiter().GetResult() is causing this.
Do you have any hints / recommendation on how to use async code in the menu actions?
So lonG Daniel
Hi @ViRuSTriNiTy,
Thanks for the compliment 😃
I try to use it with some simple async code and Serilog logging. But as soon as I have executed the first menu action the Serilog logging won't work anymore.
Could you please provide some code to reproduce the issue?
Do you have any hints / recommendation on how to use async code in the menu actions?
The menu does not support async code. My advice is to run the code synchronously by using .Result or GetAwaiter().GetResult().
+1 to get async await implemented
@joymon could you provide me a use case where async await support is needed?
Environent has a compilation setting "Treat warnings as errors" and when we use the below its showing a compilation error. "The async method lacks 'await' operators...." async void TestMenu(string[] args) { var cm = new ConsoleMenu(args, level: 2); cm.Show(); } We can solve the compilation error using the below. Task.Run(()=> cm.Show()).Wait(); It would be better if the library is natively async-await aware.
It doesn't look like a real world use case.
In the provided example, the warning can be fixed simply by removing async keyword from method declaration
void TestMenu(string[] args)
{
var cm = new ConsoleMenu(args, level: 2);
cm.Show();
}
I asked about use case because console applications are, in fact, synchronous and async Main is just a syntax sugar of .GetAwaiter().GetResult(). See https://github.com/dotnet/csharplang/issues/97#issuecomment-306522970
Thanks for the response. The sample was from an app having half a million lines of code. We will see if we can remove async.
I just released version 2.6.0 with the async API. Happy coding!
@lechu445 Works perfectly. Thank you!