public partial class Service : IService
{
#region private Variable
private Queue<Item> _downloadQueues = new Queue<Item>();
private CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource();
private async Task Initialize(string traceID)
{
await ProcessQueueAsync(traceID);
}
#endregion
#region Konstruktor
public Service()
{
string traceID = EX.GetTraceID();
_ = Initialize(traceID);
}
#endregion
#region Methoden
public async Task AddToDownloadQueue(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 ProcessQueueAsync(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();
}