Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7711069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:17:00+00:00 2026-06-01T01:17:00+00:00

I need to build a process that listen in WCF for new tasks. (Async)

  • 0

I need to build a process that listen in WCF for new tasks. (Async)
Every Task get Enqueue’ed (somehow).

What is the Best (Logical and Performance) way to loop the queue and Dequeue it.

I thought about:

while(true){
   queue.Dequeue();
}

I assume that there are better ways to do that.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T01:17:02+00:00Added an answer on June 1, 2026 at 1:17 am

    Have a look at System.Collections.Concurrent namespace – there is thread-safe queue implementation viz. ConcurrentQueue – although, I suspect that your needs would be better served by BlockingCollection.

    Blocking collection is essentially a thread-safe collection useful for producer-consumer scenario. In your case, WCF calls will act as producers that will add to the collection while the worker thread will act as consumer who would essentially take queued tasks from the collection. By using single consumer (and collection), you can ensure order of execution. If that’s not important then you may able to use multiple consumer threads. (There are also AddAny and TakeAny static overloads that will allow you to use multiple collections (multiple queues) if that is the need.)

    The advantage over while(true) approach would be avoidance of tight loop that will just consume CPU cycles. Apart from having thread-safe, this would also solve issue of synchronization between queuing and de-queuing threads.

    EDIT:

    Blocking Collection is really very simple to use. See below simple example – add task will invoked from say your WCF methods to queue up tasks while StartConsumer will be called during service start-up.

    public class MyTask { ... }
    
    private BlockingCollection<MyTask> _tasks = new BlockingCollection<MyTask>();
    
    private void AddTask(MyTask task)
    {
      _tasks.Add(task);
    }
    
    private void StartConsumer()
    {
       // I have used a task API but you can very well launch a new thread instead of task
       Task.Factory.StartNew(() =>
         {
            while (!_tasks.IsCompleted)
            {
                var task = _tasks.Take();
                ProcessTask(task);
            }
         });
    }
    

    While stopping service, one need to invoke _tasks.CompleteAdding so that consumer thread will break.

    Find more examples on MSDN:

    http://msdn.microsoft.com/en-us/library/dd997306.aspx
    http://msdn.microsoft.com/en-us/library/dd460690.aspx
    http://msdn.microsoft.com/en-us/library/dd460684.aspx

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to build a website where we display data that is refreshed every
I have files that I need cleaned up during the build process. There is
Have a build process that can't be edited and need to pack another war
I need to build a web application with different process flows and different UI
I need to build something that starts serving a H.264 encoded video to a
I have part of a build process that creates a hideously long paths in
Background I have a few scripts that run as part of my build process
I'm working in a project that already have a build process running with maven
I need to enclose special markups for the printer in the PDF build process.
HI, Have a build process using devenv.com that will compile c# code into assemblies

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.