Теперь я хотел бы убедиться, что загрузка, которую я хочу отменить, также будет отменена. На данный момент ВСЕ загрузки отменены. Что именно я делаю не так?
Большое спасибо
Код: Выделить всё
public partial class Service : IService
{
#region private Variable
private Queue _downloadQueues = new Queue();
private CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource();
private async Task Initialize(string traceID)
{
await Process(traceID);
}
#endregion
#region Konstruktor
public Service()
{
string traceID = EX.GetTraceID();
_ = Initialize(traceID);
}
#endregion
#region Methoden
public async Task Add(string traceID, Item item)
{
try
{
if (item is not null)
{
_downloadQueues.Enqueue(Item);
}
else
{
_logger.Error(traceID, $"AddToDownloadQueueAsync:: Item is null");
return;
}
}
catch (Exception ex)
{
_logger.Error(traceID, ex, $"AddDownloadToQueue:: ");
return;
}
}
public async Task CancelDownload()
{
_cancellationTokenSource.Cancel();
}
public async Task Process(string traceId)
{
while (!_cancellationTokenSource.IsCancellationRequested)
{
if (_downloadQueues.Count > 0)
{
try
{
_waitTaskEventHandler.WaitOne(Constants.WAIT_FOR_CHECKQUEUE_IN_MS);
while (_downloadQueues.TryDequeue(out Item? Item))
{
Task? task = client.Download(traceId, Item, Item.CancellationToken);
await Task.Delay(1000);
}
_waitTaskEventHandler.Reset();
}
Подробнее здесь: https://stackoverflow.com/questions/787 ... -and-queue