Is there a queue that I can wait until there is something?

mc 5,426 Reputation points
2023-06-05T00:50:04.8333333+00:00

I want to do process the orders but the order queue many empty.

while(Orders.Count>0){

}

I run this in background but it will quit if the Orders is empty

is there a queue that I can use

while(Queue.CanSeek??or )

{

}

If the queue is empty it will wait. if it is not empty it will run

Developer technologies ASP.NET ASP.NET Core
{count} votes

1 answer

Sort by: Most helpful
  1. Sreeju Nair 12,666 Reputation points
    2023-06-05T05:59:35.19+00:00

    Basically, you need your background task to be executed in an infinite loop. You may use Thread.Sleep() to ensure you don't overconsume resources. For e.g. your background service code can be something like below.

    while(! cancellationRequested)
      1. Fetch the orders
      2. Process them
      3. call Task.Delay by setting some time say 1 minute, so that orders will be processed every 1 minute, or you can leave this to small numbers, or even remove this delay to make this more real time.
    }
    
    
    
    

    I recommend you to read the following blog post that details about developing a background service using ASP.Net Core.

    https://weblogs.asp.net/sreejukg/running-background-tasks-in-asp-net-applications

    Hope this helps

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.