Skip to content

AsyncCommnad not Cancelable #59

@carsten-riedel

Description

@carsten-riedel

AsyncCommnad not Cancelable

appLifetime.ApplicationStopping will never stop even if called by an other background service or strg+c. (return 99 in my example)

I didn't found a solution right now, do you have some suggestion ?

        public class MyAsyncCommand : AsyncCommand<MyAsyncCommand.Settings>
        {
            private readonly DependencyService  dependencyService;
            private readonly IHostApplicationLifetime appLifetime;

            public MyAsyncCommand(DependencyService service, IHostApplicationLifetime appLifetime)
            {
                this.dependencyService = service;
                this.appLifetime = appLifetime;
            }

            public class Settings : CommandSettings
            {
                [Description("The shell commandline command")]
                [CommandArgument(0, "<CommandName>")]
                public string? CommandName { get; init; }
            }

            public override async Task<int> ExecuteAsync(CommandContext context, Settings settings)
            {
                dependencyService.PerformAction();
                try
                {
                    await Task.Delay(20000, appLifetime.ApplicationStopping);
                }
                catch (Exception ex)
                {
                    return 99;
                }
                return 1;
            }
        }

        public class DependencyService
        {
            public void PerformAction()
            {
                Console.WriteLine("Action performed.");
                AnsiConsole.Write(new Markup("[bold yellow]Hello[/] [red]World![/]"));
            }
        }
    }
    ```

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions