CommandLineUtils icon indicating copy to clipboard operation
CommandLineUtils copied to clipboard

[Question] How to properly create an asynchronous command?

Open MillzyDev opened this issue 4 years ago • 1 comments

I am using an API wrapper in a command declared with an attribute, the specific method I'm calling in the command is asynchronous. Upon executing the command, I get a blank statement in the console.

using Microsoft.VisualStudio.TestTools.UnitTesting;
using BeatSaverSharp;
using McMaster.Extensions.CommandLineUtils;
using System;
using System.ComponentModel.DataAnnotations;
using static PlaylistPrimeCLI.Helpers.BeatSaverHelper;

namespace PlaylistPrimeCLI.Commands
{
    [Command("create", Description = "Creates a new bplist from BeatSaver search queries")]
    internal class CreateCommand
    {
        [Option("-n|--name", Description="The name of your playlist")]
        [Required]
        public string Name { get; set; }

        private async void OnExecute(CommandLineApplication app)
        {
            var query = await Client.SearchBeatmaps(new SearchTextFilterOption // async method
            {
                NoodleExtensions = true,
                Chroma = true
            });

            Assert.IsNotNull(query);
            Console.WriteLine(query.BeatMaps[0].Key); // (bool value) nothing gets printed

            Console.WriteLine(Name); // nothing gets printed
        }
    }
}

Any guidance on resolving this issue is very much appreciated

MillzyDev avatar Sep 16 '21 20:09 MillzyDev

Don't use async void. Use async Task instead. https://haacked.com/archive/2014/11/11/async-void-methods/

natemcmaster avatar Sep 27 '21 02:09 natemcmaster

This issue has been automatically marked as stale because it has no recent activity. It will be closed if no further activity occurs. Please comment if you believe this should remain open, otherwise it will be closed in 14 days. Thank you for your contributions to this project.

github-actions[bot] avatar Sep 27 '22 02:09 github-actions[bot]

Closing due to inactivity. If you are looking at this issue in the future and think it should be reopened, please make a commented here and mention natemcmaster so he sees the notification.

github-actions[bot] avatar Oct 11 '22 02:10 github-actions[bot]