ConsoleMenu icon indicating copy to clipboard operation
ConsoleMenu copied to clipboard

How to use this library with async actions

Open ViRuSTriNiTy opened this issue 3 years ago • 6 comments

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

ViRuSTriNiTy avatar May 02 '22 08:05 ViRuSTriNiTy

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().

lechu445 avatar May 02 '22 10:05 lechu445

+1 to get async await implemented

joymon avatar Jul 25 '22 00:07 joymon

@joymon could you provide me a use case where async await support is needed?

lechu445 avatar Aug 06 '22 22:08 lechu445

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.

joymon avatar Aug 10 '22 21:08 joymon

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

lechu445 avatar Aug 10 '22 22:08 lechu445

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.

joymon avatar Sep 07 '22 12:09 joymon

I just released version 2.6.0 with the async API. Happy coding!

lechu445 avatar Feb 17 '23 21:02 lechu445

@lechu445 Works perfectly. Thank you!

ViRuSTriNiTy avatar Feb 21 '23 09:02 ViRuSTriNiTy