How to stop processing task

  Kiến thức lập trình

how to stop an “ex” like that. The canceltaion token is not always supported in Execute method. I would always like to kill task.

try
            {
                var ex = Execute(parameters, cancellationToken, performContext);

                await Task.WhenAny(ex
                    ,
                    ThrowIfCancellationRequested(ex, cancellationToken));

                return;
            }
            catch (Exception ex)
            {
                _loggerService.LogError(ex, "JobBase execute internal with raw params without job semaphore.");
                throw;
            }

private async Task ThrowIfCancellationRequested(Task task, CancellationToken cancellationToken)
{
try
{
while (true)
{
await Task.Delay(5000, cancellationToken);
cancellationToken.ThrowIfCancellationRequested();
}
}
catch
{
task.Dispose();
}
}

    protected override async Task Execute(Parameter parameter,
        CancellationToken cancellationToken,
        PerformContext performContext = null)
    {
        while (true)
        {
            await Task.Delay(TimeSpan.FromSeconds(10));
        }
    }

próbwałem chyba wszystkiego i nie mam pojęcią jak ubić zadanie.

methoda execute cały czas działa pomimo tego ze robie dispose na catch

New contributor

krd is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT