natemcmaster/CommandLineUtils

[Question] How to properly create an asynchronous command?

MillzyDev opened this issue · 3 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

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

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.

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.