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 4559752
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T17:50:00+00:00 2026-05-21T17:50:00+00:00

I am writing a WCF service that receives notifications from several modules (DB, other

  • 0

I am writing a WCF service that receives notifications from several modules (DB, other services..) and adds them to a blocking collection, to be processed on a consumer thread that publishes the relevant data to the clients.

A client can request the full data stored on the server, and during this operation i don’t want to accept any new notifications. Basically i want to suspend the blocking collection (or the consumer thread) and resume the notification receiving and processing after completing the client’s request.

What is a good way to implement this behavior?

  • 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-05-21T17:50:01+00:00Added an answer on May 21, 2026 at 5:50 pm

    You can wrap the BlockingCollection and add a Queue for the producer and some sync mechanism internally.

    here is a sample:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows.Forms;
    using System.Threading;
    using System.Collections;
    using System.Collections.Concurrent;
    
    namespace WindowsFormsApplication2
    {
        static class Program
        {
            private static void Producer()
            {
                int i = 0;
                while (i < 500)
                {
                    Thread.Sleep(50); // can remove the sleep just for debugging
                    bc.ProducerAdd("Notification " + i);
                    i++;
                }
            }
    
            private static void Consumer()
            {
                while (true)
                {
                    foreach (var it in bc)
                    {
                        Console.WriteLine(String.Format("{0} : CONSUMES {1}", Thread.CurrentThread.Name, it));
    
                        // this is just a check test
                        lock (consumed)
                        {
                            consumed.Add(it, Convert.ToInt32(it.Split(' ')[1])); // this will fail if we consume the same twice
                        }
                    }
                }
            }
    
            private static void TogglePause()
            {
                while (true)
                {
                    Thread.Sleep(3000); //every 3 seconds
                    bc.Paused = !bc.Paused;
                    Console.WriteLine("PAUSE is now: " + bc.Paused);
                }
            }
    
            private static QueuedBlockingCollection<string> bc = new QueuedBlockingCollection<string>();
            private static Dictionary<string, int> consumed = new Dictionary<string, int>();
    
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main()
            {
                Thread producer = new Thread(Producer);
                producer.Start();
    
                Thread consumer1 = new Thread(Consumer);
                consumer1.Name = "Consumer 1";
                consumer1.Start();
    
                Thread consumer2 = new Thread(Consumer);
                consumer2.Name = "Consumer 2";
                consumer2.Start();
    
                Thread pauser = new Thread(TogglePause);
                pauser.Start();
    
                while (true)
                {
                    // wait and observe console writelines
                    Application.DoEvents();
                }
            }
        }
    
        class QueuedBlockingCollection<T> : IEnumerable<T>, ICollection, IEnumerable, IDisposable
        {
            private Queue<T> queue = new Queue<T>();
            private BlockingCollection<T> collection = new BlockingCollection<T>();
            private Thread syncThread;
    
            public bool Paused { get; set; }
            public bool Exiting { get; set; }
    
            private void SyncLoop()
            {
                // this while will wait for the class to be destroyed
                while (!Exiting)
                {
                    try
                    {
                        // this while will finish when queue is synched to collection, or paused or exiting
                        while (queue.Count > 0 && !Exiting && !Paused)
                        {
                            lock (collection)
                            {
                                T item = queue.Dequeue();
                                collection.Add(item);
                                Console.WriteLine(String.Format("SYNCHED {0} TO COLLECTION", item));
                            }
                        }
                    }
                    catch (ObjectDisposedException)
                    {
                        // collection has been disposed, exit this thread
                        break;
                    }
                }
            }
    
            public void ProducerAdd(T item)
            {
                // producer always adds to the queue if the collection is paused so it wont block the collection
                // the sync thread will block the collection only when adding from the queue
                // consumers automatically block this collection when enumerating because it is a wrapper to the internal blocked coll...
    
                if (!Paused)
                {
                    lock (collection)
                    {
                        collection.Add(item);
                        Console.WriteLine(String.Format("Producer ADDED '" + item + "', status collection {0} queue {1}", this.Count, queue.Count));
                    }
                }
                else
                {
                    queue.Enqueue(item);
                    Console.WriteLine(String.Format("Producer ENQUEUED '" + item + "', Status -> Collection: {0}, Queue: {1}", this.Count, queue.Count));
                }
            }
    
            public QueuedBlockingCollection()
            {
                //collection.CompleteAdding();
                syncThread = new Thread(SyncLoop);
                syncThread.Start();
            }
    
            ~QueuedBlockingCollection()
            {
                Exiting = true;
                syncThread.Join(200);
            }
    
            public IEnumerator<T> GetEnumerator()
            {
                return collection.GetConsumingEnumerable().GetEnumerator();
            }
    
            IEnumerator IEnumerable.GetEnumerator()
            {
                return collection.GetConsumingEnumerable().GetEnumerator();
            }
    
            public void CopyTo(Array array, int index)
            {
                var items = collection.GetConsumingEnumerable();
                int offset = 0;
    
                if (array == null) throw new NullReferenceException("Array must be initialized");
                if (array.Rank > 1) throw new InvalidOperationException("Array must have 1 dimension");
                if (array.GetLength(0) - index < items.Count()) throw new IndexOutOfRangeException("Array is too small");
    
                foreach (var item in items)
                {
                    array.SetValue(item, index + offset);
                    offset++;
                }
    
            }
    
            public int Count
            {
                get
                {
                    return collection.Count;
                }
            }
    
            public bool IsSynchronized
            {
                get { return queue.Count == 0; }
            }
    
            public object SyncRoot
            {
                get { return collection; }
            }
    
            public void Dispose()
            {
                collection.Dispose();
            }
    
        }
    }
    

    You can add the BlockingCollection methods you need by just doing them as a wrapper to collection methods.

    Here is some output:

    Producer ADDED 'Notification 0', status collection 1 queue 0
    Producer ADDED 'Notification 1', status collection 2 queue 0
    Producer ADDED 'Notification 2', status collection 3 queue 0
    Producer ADDED 'Notification 3', status collection 4 queue 0
    Consumer 2 : CONSUMES Notification 0
    Consumer 2 : CONSUMES Notification 1
    Consumer 2 : CONSUMES Notification 2
    Consumer 2 : CONSUMES Notification 3
    Consumer 2 : CONSUMES Notification 4
    Producer ADDED 'Notification 4', status collection 0 queue 0
    Producer ADDED 'Notification 5', status collection 1 queue 0
    Consumer 1 : CONSUMES Notification 5
    Producer ADDED 'Notification 6', status collection 1 queue 0
    Consumer 2 : CONSUMES Notification 6
    Producer ADDED 'Notification 7', status collection 1 queue 0
    Consumer 1 : CONSUMES Notification 7
    Producer ADDED 'Notification 8', status collection 1 queue 0
    Consumer 2 : CONSUMES Notification 8
    Producer ADDED 'Notification 9', status collection 1 queue 0
    Consumer 1 : CONSUMES Notification 9
    Producer ADDED 'Notification 10', status collection 1 queue 0
    Consumer 2 : CONSUMES Notification 10
    Producer ADDED 'Notification 11', status collection 1 queue 0
    Consumer 1 : CONSUMES Notification 11
    Consumer 2 : CONSUMES Notification 12
    Producer ADDED 'Notification 12', status collection 0 queue 0
    Producer ADDED 'Notification 13', status collection 1 queue 0
    Consumer 1 : CONSUMES Notification 13
    Producer ADDED 'Notification 14', status collection 1 queue 0
    Consumer 2 : CONSUMES Notification 14
    Producer ADDED 'Notification 15', status collection 1 queue 0
    Consumer 1 : CONSUMES Notification 15
    Producer ADDED 'Notification 16', status collection 1 queue 0
    Consumer 2 : CONSUMES Notification 16
    Producer ADDED 'Notification 17', status collection 1 queue 0
    Consumer 1 : CONSUMES Notification 17
    Producer ADDED 'Notification 18', status collection 1 queue 0
    Consumer 2 : CONSUMES Notification 18
    Producer ADDED 'Notification 19', status collection 1 queue 0
    Consumer 1 : CONSUMES Notification 19
    Producer ADDED 'Notification 20', status collection 1 queue 0
    Consumer 2 : CONSUMES Notification 20
    Producer ADDED 'Notification 21', status collection 1 queue 0
    Consumer 1 : CONSUMES Notification 21
    Producer ADDED 'Notification 22', status collection 1 queue 0
    Consumer 2 : CONSUMES Notification 22
    Producer ADDED 'Notification 23', status collection 1 queue 0
    Consumer 1 : CONSUMES Notification 23
    Producer ADDED 'Notification 24', status collection 1 queue 0
    Consumer 2 : CONSUMES Notification 24
    Producer ADDED 'Notification 25', status collection 1 queue 0
    Consumer 1 : CONSUMES Notification 25
    Producer ADDED 'Notification 26', status collection 1 queue 0
    Consumer 2 : CONSUMES Notification 26
    Producer ADDED 'Notification 27', status collection 1 queue 0
    Consumer 1 : CONSUMES Notification 27
    Producer ADDED 'Notification 28', status collection 1 queue 0
    Consumer 2 : CONSUMES Notification 28
    Producer ADDED 'Notification 29', status collection 1 queue 0
    Consumer 1 : CONSUMES Notification 29
    Producer ADDED 'Notification 30', status collection 1 queue 0
    Consumer 2 : CONSUMES Notification 30
    Producer ADDED 'Notification 31', status collection 1 queue 0
    Consumer 1 : CONSUMES Notification 31
    Producer ADDED 'Notification 32', status collection 1 queue 0
    Consumer 2 : CONSUMES Notification 32
    Producer ADDED 'Notification 33', status collection 1 queue 0
    Consumer 1 : CONSUMES Notification 33
    Producer ADDED 'Notification 34', status collection 1 queue 0
    Consumer 2 : CONSUMES Notification 34
    Producer ADDED 'Notification 35', status collection 1 queue 0
    Consumer 1 : CONSUMES Notification 35
    Producer ADDED 'Notification 36', status collection 1 queue 0
    Consumer 2 : CONSUMES Notification 36
    Producer ADDED 'Notification 37', status collection 1 queue 0
    Consumer 1 : CONSUMES Notification 37
    Producer ADDED 'Notification 38', status collection 1 queue 0
    Consumer 2 : CONSUMES Notification 38
    Producer ADDED 'Notification 39', status collection 1 queue 0
    Consumer 1 : CONSUMES Notification 39
    Producer ADDED 'Notification 40', status collection 1 queue 0
    Consumer 2 : CONSUMES Notification 40
    Producer ADDED 'Notification 41', status collection 1 queue 0
    Consumer 1 : CONSUMES Notification 41
    Producer ADDED 'Notification 42', status collection 1 queue 0
    Consumer 2 : CONSUMES Notification 42
    Producer ADDED 'Notification 43', status collection 1 queue 0
    Consumer 1 : CONSUMES Notification 43
    Producer ADDED 'Notification 44', status collection 1 queue 0
    Consumer 2 : CONSUMES Notification 44
    Producer ADDED 'Notification 45', status collection 1 queue 0
    Consumer 1 : CONSUMES Notification 45
    Producer ADDED 'Notification 46', status collection 1 queue 0
    Consumer 2 : CONSUMES Notification 46
    Producer ADDED 'Notification 47', status collection 1 queue 0
    Consumer 1 : CONSUMES Notification 47
    Producer ADDED 'Notification 48', status collection 1 queue 0
    Consumer 2 : CONSUMES Notification 48
    Producer ADDED 'Notification 49', status collection 1 queue 0
    Consumer 1 : CONSUMES Notification 49
    Producer ADDED 'Notification 50', status collection 1 queue 0
    Consumer 2 : CONSUMES Notification 50
    Producer ADDED 'Notification 51', status collection 1 queue 0
    Consumer 1 : CONSUMES Notification 51
    Producer ADDED 'Notification 52', status collection 1 queue 0
    Consumer 2 : CONSUMES Notification 52
    Producer ADDED 'Notification 53', status collection 1 queue 0
    Consumer 1 : CONSUMES Notification 53
    Producer ADDED 'Notification 54', status collection 1 queue 0
    Consumer 2 : CONSUMES Notification 54
    Producer ADDED 'Notification 55', status collection 1 queue 0
    Consumer 1 : CONSUMES Notification 55
    Producer ADDED 'Notification 56', status collection 1 queue 0
    Consumer 2 : CONSUMES Notification 56
    Producer ADDED 'Notification 57', status collection 1 queue 0
    Consumer 1 : CONSUMES Notification 57
    Producer ADDED 'Notification 58', status collection 1 queue 0
    Consumer 2 : CONSUMES Notification 58
    Producer ADDED 'Notification 59', status collection 1 queue 0
    Consumer 1 : CONSUMES Notification 59
    Producer ADDED 'Notification 60', status collection 1 queue 0
    Consumer 2 : CONSUMES Notification 60
    PAUSE is now: True
    Producer ENQUEUED 'Notification 61', Status -> Collection: 0, Queue: 1
    Producer ENQUEUED 'Notification 62', Status -> Collection: 0, Queue: 2
    Producer ENQUEUED 'Notification 63', Status -> Collection: 0, Queue: 3
    Producer ENQUEUED 'Notification 64', Status -> Collection: 0, Queue: 4
    Producer ENQUEUED 'Notification 65', Status -> Collection: 0, Queue: 5
    Producer ENQUEUED 'Notification 66', Status -> Collection: 0, Queue: 6
    Producer ENQUEUED 'Notification 67', Status -> Collection: 0, Queue: 7
    Producer ENQUEUED 'Notification 68', Status -> Collection: 0, Queue: 8
    Producer ENQUEUED 'Notification 69', Status -> Collection: 0, Queue: 9
    Producer ENQUEUED 'Notification 70', Status -> Collection: 0, Queue: 10
    Producer ENQUEUED 'Notification 71', Status -> Collection: 0, Queue: 11
    Producer ENQUEUED 'Notification 72', Status -> Collection: 0, Queue: 12
    Producer ENQUEUED 'Notification 73', Status -> Collection: 0, Queue: 13
    Producer ENQUEUED 'Notification 74', Status -> Collection: 0, Queue: 14
    Producer ENQUEUED 'Notification 75', Status -> Collection: 0, Queue: 15
    Producer ENQUEUED 'Notification 76', Status -> Collection: 0, Queue: 16
    Producer ENQUEUED 'Notification 77', Status -> Collection: 0, Queue: 17
    Producer ENQUEUED 'Notification 78', Status -> Collection: 0, Queue: 18
    Producer ENQUEUED 'Notification 79', Status -> Collection: 0, Queue: 19
    Producer ENQUEUED 'Notification 80', Status -> Collection: 0, Queue: 20
    Producer ENQUEUED 'Notification 81', Status -> Collection: 0, Queue: 21
    Producer ENQUEUED 'Notification 82', Status -> Collection: 0, Queue: 22
    Producer ENQUEUED 'Notification 83', Status -> Collection: 0, Queue: 23
    Producer ENQUEUED 'Notification 84', Status -> Collection: 0, Queue: 24
    Producer ENQUEUED 'Notification 85', Status -> Collection: 0, Queue: 25
    Producer ENQUEUED 'Notification 86', Status -> Collection: 0, Queue: 26
    Producer ENQUEUED 'Notification 87', Status -> Collection: 0, Queue: 27
    Producer ENQUEUED 'Notification 88', Status -> Collection: 0, Queue: 28
    Producer ENQUEUED 'Notification 89', Status -> Collection: 0, Queue: 29
    Producer ENQUEUED 'Notification 90', Status -> Collection: 0, Queue: 30
    Producer ENQUEUED 'Notification 91', Status -> Collection: 0, Queue: 31
    Producer ENQUEUED 'Notification 92', Status -> Collection: 0, Queue: 32
    Producer ENQUEUED 'Notification 93', Status -> Collection: 0, Queue: 33
    Producer ENQUEUED 'Notification 94', Status -> Collection: 0, Queue: 34
    Producer ENQUEUED 'Notification 95', Status -> Collection: 0, Queue: 35
    Producer ENQUEUED 'Notification 96', Status -> Collection: 0, Queue: 36
    Producer ENQUEUED 'Notification 97', Status -> Collection: 0, Queue: 37
    The thread '<No Name>' (0x1100) has exited with code 0 (0x0).
    Producer ENQUEUED 'Notification 98', Status -> Collection: 0, Queue: 38
    Producer ENQUEUED 'Notification 99', Status -> Collection: 0, Queue: 39
    PAUSE is now: False
    Consumer 1 : CONSUMES Notification 61
    SYNCHED Notification 61 TO COLLECTION
    SYNCHED Notification 62 TO COLLECTION
    Consumer 2 : CONSUMES Notification 62
    Consumer 1 : CONSUMES Notification 63
    SYNCHED Notification 63 TO COLLECTION
    SYNCHED Notification 64 TO COLLECTION
    Consumer 2 : CONSUMES Notification 64
    Consumer 1 : CONSUMES Notification 65
    SYNCHED Notification 65 TO COLLECTION
    SYNCHED Notification 66 TO COLLECTION
    Consumer 2 : CONSUMES Notification 66
    Consumer 1 : CONSUMES Notification 67
    SYNCHED Notification 67 TO COLLECTION
    SYNCHED Notification 68 TO COLLECTION
    Consumer 2 : CONSUMES Notification 68
    Consumer 1 : CONSUMES Notification 69
    SYNCHED Notification 69 TO COLLECTION
    SYNCHED Notification 70 TO COLLECTION
    Consumer 2 : CONSUMES Notification 70
    Consumer 1 : CONSUMES Notification 71
    SYNCHED Notification 71 TO COLLECTION
    SYNCHED Notification 72 TO COLLECTION
    Consumer 2 : CONSUMES Notification 72
    Consumer 1 : CONSUMES Notification 73
    SYNCHED Notification 73 TO COLLECTION
    SYNCHED Notification 74 TO COLLECTION
    SYNCHED Notification 75 TO COLLECTION
    SYNCHED Notification 76 TO COLLECTION
    SYNCHED Notification 77 TO COLLECTION
    SYNCHED Notification 78 TO COLLECTION
    SYNCHED Notification 79 TO COLLECTION
    SYNCHED Notification 80 TO COLLECTION
    SYNCHED Notification 81 TO COLLECTION
    SYNCHED Notification 82 TO COLLECTION
    SYNCHED Notification 83 TO COLLECTION
    SYNCHED Notification 84 TO COLLECTION
    SYNCHED Notification 85 TO COLLECTION
    SYNCHED Notification 86 TO COLLECTION
    SYNCHED Notification 87 TO COLLECTION
    SYNCHED Notification 88 TO COLLECTION
    SYNCHED Notification 89 TO COLLECTION
    SYNCHED Notification 90 TO COLLECTION
    SYNCHED Notification 91 TO COLLECTION
    SYNCHED Notification 92 TO COLLECTION
    SYNCHED Notification 93 TO COLLECTION
    SYNCHED Notification 94 TO COLLECTION
    SYNCHED Notification 95 TO COLLECTION
    SYNCHED Notification 96 TO COLLECTION
    SYNCHED Notification 97 TO COLLECTION
    SYNCHED Notification 98 TO COLLECTION
    SYNCHED Notification 99 TO COLLECTION
    Consumer 1 : CONSUMES Notification 74
    Consumer 2 : CONSUMES Notification 75
    Consumer 1 : CONSUMES Notification 76
    Consumer 2 : CONSUMES Notification 77
    Consumer 2 : CONSUMES Notification 79
    Consumer 1 : CONSUMES Notification 78
    Consumer 2 : CONSUMES Notification 80
    Consumer 1 : CONSUMES Notification 81
    Consumer 2 : CONSUMES Notification 82
    Consumer 1 : CONSUMES Notification 83
    Consumer 2 : CONSUMES Notification 84
    Consumer 1 : CONSUMES Notification 85
    Consumer 2 : CONSUMES Notification 86
    Consumer 1 : CONSUMES Notification 87
    Consumer 2 : CONSUMES Notification 88
    Consumer 1 : CONSUMES Notification 89
    Consumer 2 : CONSUMES Notification 90
    Consumer 1 : CONSUMES Notification 91
    Consumer 2 : CONSUMES Notification 92
    Consumer 1 : CONSUMES Notification 93
    Consumer 2 : CONSUMES Notification 94
    Consumer 1 : CONSUMES Notification 95
    Consumer 2 : CONSUMES Notification 96
    Consumer 1 : CONSUMES Notification 97
    Consumer 2 : CONSUMES Notification 98
    Consumer 1 : CONSUMES Notification 99
    PAUSE is now: True
    PAUSE is now: False
    

    This is thread safe…

    So you could use X producers and Y consumers if you wish…

    If the blocking on ProducerAdd for more producers is a no go then you can use

    Monitor.TryEnter(collection)
    

    instead of

    lock (collection)
    

    and if it fails use

    queue.Enqueue()
    

    which will be synched back to the collection as soon the locks have been released

    Or if you wish just use queu.Enqueue() always for any producer and let the sync thread do its job…

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

Sidebar

Related Questions

Put simply, I have a WCF service that manages apples. Apart from other functionality,
I'm writing a WCF service that receives events. It's to an agreed standard so
I'm writing a middle-tier WCF service that needs to fetch some data from a
I am writing WCF service that uses wsHttpBinding binding, which is not hosted in
I have the following data model: I am writing a WCF service that needs
I have a WCF service that is responsible for writing a log file. I
I am writing a WCF webservice. I need to access the service from another
I'm in the process of writing a WCF service that will allow an ASP.Net
I'm a bit lost concerning WCF Server authentication... I'm writing a WCF service that
I'm currently writing a WCF web service that uses LINQ to SQL to fetch

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.