Threading Consumers in a Blocking Collection

122 Views Asked by At

I have windows service that on a timer event adds items to a blocking collection and then creates a thread for each item in the queue to be processed.I am using CompleteAdding() to make sure the queue is not empty but I am still not sure if for ex 1. If this method is thread safe 2. Does GetConsumingEnumerable() leave the consumer threads waiting for more work indefinitely? (I want the threads to exit when processing is complete) 3. Is this solution ideal for a production environment?

dt = GetValues()

            If Not dt Is Nothing Then

                Dim value As Integer

                For Each dr As DataRow In dt.Rows

                    BlockingColl.Add(value)
                Next
                BlockingColl.CompleteAdding()

             Try
                    'Create consumer threads

                    While BlockingColl.IsCompleted = False

                        For Each value As Integer In BlockingColl.GetConsumingEnumerable()
                            Task.Factory.StartNew(Sub()
                                                      If (value IsNot Nothing) Then
                                                          ProcessValue(value)
                                                      End If
                                                  End Sub)
                        Next
                    End While
            Catch e As InvalidOperationException
               throw ex
            End Try

            End if
1

There are 1 best solutions below

2
On

This seems like a complicated way to write Parallel.ForEach(dt.Rows, dr => ProcessValue(dr)).

Probably, you need to limit the degree of parallelism as well because the heuristics of Parallel.ForEach tend to create crazy amounts of threads.