C
C#6mo ago
UltraWelfare

✅ Waiting on a task more efficiently

I've made this class for my application that needs to process some things in the background using a queue:
c#
public class IcsBackgroundQueue
{
private ConcurrentQueue<IcsCashDesk> _jobQueue = new();

private OrdersDB _ordersDb = new(Types.Order.sell);
private Task _processingTask;
private CancellationTokenSource _cts = new();

public IcsBackgroundWorker()
{
_processingTask = Task.Run(ProcessJobs, _cts.Token);
}

// ... stop function, enqueue function etc

private async Task ProcessJobs()
{
while (!_cts.IsCancellationRequested)
{
try
{
if (_jobQueue.IsEmpty)
{
await Task.Delay(1000, _cts.Token);
continue;
}

_jobQueue.TryDequeue(out var cashDesk);
if (cashDesk == null) continue;
await cashDesk.CompleteWaitAsync();
}
catch (OperationCanceledException)
{
}
}
}
}
c#
public class IcsBackgroundQueue
{
private ConcurrentQueue<IcsCashDesk> _jobQueue = new();

private OrdersDB _ordersDb = new(Types.Order.sell);
private Task _processingTask;
private CancellationTokenSource _cts = new();

public IcsBackgroundWorker()
{
_processingTask = Task.Run(ProcessJobs, _cts.Token);
}

// ... stop function, enqueue function etc

private async Task ProcessJobs()
{
while (!_cts.IsCancellationRequested)
{
try
{
if (_jobQueue.IsEmpty)
{
await Task.Delay(1000, _cts.Token);
continue;
}

_jobQueue.TryDequeue(out var cashDesk);
if (cashDesk == null) continue;
await cashDesk.CompleteWaitAsync();
}
catch (OperationCanceledException)
{
}
}
}
}
Is there anything to do to better optimize the while loop when no jobs are in the queue? I've done a simple 1s delay but I'd bet there's a better way of doing it..?
7 Replies
UltraWelfare
UltraWelfareOP6mo ago
Actually I thought of using a semaphore to do it:
public void EnqueueJob(IcsCashDesk cashDesk)
{
_jobQueue.Enqueue(cashDesk);
_signal.Release();
}

private async Task ProcessJobs()
{
while (!_cts.IsCancellationRequested)
{
try
{
await _signal.WaitAsync(_cts.Token);
if (_jobQueue.IsEmpty) continue;
while (_jobQueue.TryDequeue(out var cashDesk))
{
await cashDesk.CompleteWaitAsync();
}
}
catch (OperationCanceledException)
{
}
}
}
public void EnqueueJob(IcsCashDesk cashDesk)
{
_jobQueue.Enqueue(cashDesk);
_signal.Release();
}

private async Task ProcessJobs()
{
while (!_cts.IsCancellationRequested)
{
try
{
await _signal.WaitAsync(_cts.Token);
if (_jobQueue.IsEmpty) continue;
while (_jobQueue.TryDequeue(out var cashDesk))
{
await cashDesk.CompleteWaitAsync();
}
}
catch (OperationCanceledException)
{
}
}
}
cap5lut
cap5lut6mo ago
hey, i think i just answered this in #help by recommending channels, right? if so and u dont have further questions, please $close this thread 🙂
MODiX
MODiX6mo ago
If you have no further questions, please use /close to mark the forum thread as answered
UltraWelfare
UltraWelfareOP6mo ago
$close
MODiX
MODiX6mo ago
If you have no further questions, please use /close to mark the forum thread as answered
UltraWelfare
UltraWelfareOP6mo ago
rip
No description
cap5lut
cap5lut6mo ago
lmao xD well at least its clear now for others that this is already resolved ;p
Want results from more Discord servers?
Add your server