C
C#5mo ago
Steff

Queue Cancellationtoken

Hello! I have a little Problem I have a queue und want break a Download. In my item class is a canceltoken and I have in Service a cancellationsource. But If i break a Download up, all Downloads failed. But I want one download who I take the button press using the button Can anyone help me please? Here is a Code
10 Replies
Steff
SteffOP5mo ago
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();
}
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();
}
Anton
Anton5mo ago
how are items created
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
Steff
SteffOP5mo ago
So I will probably rewrite the entire service again But this is my Item Class:
public partial class Item : ObservableObject
{
[ObservableProperty]
public string fileName;

[ObservableProperty]
public Decimal progress;

[ObservableProperty]
public double totalBytes;

[ObservableProperty]
public Customer downloadCustomer;

[ObservableProperty]
public StatusDownloads status;

[ObservableProperty]
public CancellationToken cancellationToken;

[ObservableProperty]
public CancellationTokenSource cancellationTokenSource;
public partial class Item : ObservableObject
{
[ObservableProperty]
public string fileName;

[ObservableProperty]
public Decimal progress;

[ObservableProperty]
public double totalBytes;

[ObservableProperty]
public Customer downloadCustomer;

[ObservableProperty]
public StatusDownloads status;

[ObservableProperty]
public CancellationToken cancellationToken;

[ObservableProperty]
public CancellationTokenSource cancellationTokenSource;
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
Steff
SteffOP5mo ago
I am new to this field so please excuse my ignorance. So i can delete [ObservableProperty] from the CancellationToken and delete CancallationTokenSource, right ?
sibber
sibber5mo ago
do you know what observable means? it means it will send notifications when the thing changes so when you mark property as observable, it will send a notification when you set it thats usually only used for ui updates
Steff
SteffOP5mo ago
oh shit. I am really stupid
sibber
sibber5mo ago
no youre just learning
Steff
SteffOP5mo ago
Thanks all 🙂 ❤️
Want results from more Discord servers?
Add your server