blockingcollection

c# Blocking Collection and Threading

Deadly 提交于 2020-08-08 05:44:08
问题 I'm new to using Blocking Collection and threading and want to make sure I'm following best practice. I'm using a third party API that is not thread safe. I will be making multiple simultaneous requests to the API, so I need to add these requests to a queue and process them one after another. To do this I have a blocking collection: BlockingCollection<myEventArgs> myTasks = new BlockingCollection<myEventArgs>(); private void myEventHandler(object sender, myEventArgs e) { myTasks.Add(e); }

Producer/ Consumer pattern using threads and EventWaitHandle

久未见 提交于 2020-01-23 01:22:08
问题 I guess it is sort of a code review, but here is my implementation of the producer / consumer pattern. What I would like to know is would there be a case in which the while loops in the ReceivingThread() or SendingThread() methods might stop executing. Please note that EnqueueSend(DataSendEnqeueInfo info) is called from multiple different threads and I probably can't use tasks here since I definitely have to consume commands in a separate thread. private Thread mReceivingThread; private

Why is this causing an ArgumentOutOfRangeException when using Parallel.For?

Deadly 提交于 2020-01-15 23:45:22
问题 I have had a go at writing something to hash numbers and check them against a list to see if a matching hash exists or not. I got this working fine using a for loop, I then decided to try speed things up by using Parallel.For - unfortunately this causes an ArgumentOutOfRangeException that i'm having trouble debugging. public class HashCompare { private string encryptedCardNumber; private Encrypter encrypter; private BlockingCollection<string> paraCardNumberList; public string Compare(string

c# wpf Updating UI source from BlockingCollection with Dispatcher

怎甘沉沦 提交于 2020-01-11 14:01:14
问题 Here's my problem. I'm loading a few BitmapImages in a BlockingCollection public void blockingProducer(BitmapImage imgBSource) { if (!collection.IsAddingCompleted) collection.Add(imgBSource); } the loading happens in a backgroungwork thread. private void worker_DoWork(object sender, DoWorkEventArgs e) { String filepath; int imgCount = 0; for (int i = 1; i < 10; i++) { imgCount++; filepath = "Snap"; filepath += imgCount; filepath += ".bmp"; this.Dispatcher.BeginInvoke(new Action(() => { label1

Why does iterating over GetConsumingEnumerable() not fully empty the underlying blocking collection

て烟熏妆下的殇ゞ 提交于 2019-12-31 12:32:24
问题 I have a quantifiable & repeatable problem using the Task Parallel Library, BlockingCollection<T> , ConcurrentQueue<T> & GetConsumingEnumerable while trying to create a simple pipeline. In a nutshell, adding entries to a default BlockingCollection<T> (which under the hood is relying on a ConcurrentQueue<T> ) from one thread, does not guarantee that they will be popped off the BlockingCollection<T> from another thread calling the GetConsumingEnumerable() Method. I've created a very simple

Why does iterating over GetConsumingEnumerable() not fully empty the underlying blocking collection

烈酒焚心 提交于 2019-12-31 12:31:26
问题 I have a quantifiable & repeatable problem using the Task Parallel Library, BlockingCollection<T> , ConcurrentQueue<T> & GetConsumingEnumerable while trying to create a simple pipeline. In a nutshell, adding entries to a default BlockingCollection<T> (which under the hood is relying on a ConcurrentQueue<T> ) from one thread, does not guarantee that they will be popped off the BlockingCollection<T> from another thread calling the GetConsumingEnumerable() Method. I've created a very simple

Consumer/Producer with BlockingCollection appears slow

雨燕双飞 提交于 2019-12-25 05:07:21
问题 I am getting data from an external socket connection through the "Producer" below. I place the data into a BlockingCollection , which is then read by the consumer. If the consumer does NOT receive data within a fixed period, it fires off anyway, such that my ProcessDataOnGrid , does something whenever data arrives OR AT LEAST after x millisecs. The problem is that I have read that BlockingCollection is the preferred approach for this, BUT is appears very slow. On average 150ms between when I

Observing changes in a BlockingCollection without consuming

只愿长相守 提交于 2019-12-24 14:28:17
问题 A consumer thread and multiple producer threads synchronize their work with a System.Collections.Concurrent.BlockingCollection<T>. The producers call blockingCollection.Add() and the consumer runs foreach (var item in blockingCollection.GetConsumingEnumerable()) {...} Now one producer wants to "flush" the buffer: The producer wants to wait until all current items have been consumed. Other producers may add further items in the meantime, but this producer is only interested in the items that

blocking collection process n items at a time - continuing as soon as 1 is done

前提是你 提交于 2019-12-22 09:33:06
问题 I have the following Scenario. I take 50 jobs from the database into a blocking collection. Each job is a long running one. (potentially could be). So I want to run them in a separate thread. (I know - it may be better to run them as Task.WhenAll and let the TPL figure it out - but I want to control how many runs simultaneously) Say I want to run 5 of them simultaneously (configurable) I create 5 tasks (TPL), one for each job and run them in parallel. What I want to do is to pick up the next

How to cancel GetConsumingEnumerable() on BlockingCollection

痞子三分冷 提交于 2019-12-21 04:21:31
问题 In the following code I'm using the CancellationToken to wake up the GetConsumingEnumerable() when the producer is not producing and I want to break out of the foreach and exit the Task. But I dont see IsCancellationRequested being logged and my Task.Wait(timeOut) waits for the full timeOut period. What am I doing wrong? userToken.Task = Task.Factory.StartNew(state => { userToken.CancelToken = new CancellationTokenSource(); foreach (var broadcast in userToken.BroadcastQueue