blockingcollection

How to cancel GetConsumingEnumerable() on BlockingCollection

早过忘川 提交于 2019-12-21 04:21:05
问题 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

Is the list order of a ConcurrentDictionary guaranteed?

人盡茶涼 提交于 2019-12-20 03:41:15
问题 I am using a ConcurrentDictionary to store log-lines, and when I need to display them to the user I call ToList() to generate a list. But the weird thing is that some users receive the most recent lines first in the list, while they should logically be last. Is this because ConcurrentDictionary doesnt guarantee a persistent order on the IEnumerate interface, or what can be the reason? 回答1: No ConcurrentDictionary (and Dictionary<T> for that matter) does not guarantee the ordering of the keys

awaiting async method in consumer using BlockingCollection as queue

拟墨画扇 提交于 2019-12-11 04:47:07
问题 I am working on a server side console application which receives data from multiple WCF services, does some work on it and then forwards the results to an IIS server over a single connection using SignalR. I tried to implement this using the producer consumer pattern where the WCF services are the producers and the class sending the data using SignalR is the consumer. For the queue I used the BlockingCollection. However, when using await/async to send the data in the consumer's while loop

Producer Consumer Separate Classes with common BlockingCollection

荒凉一梦 提交于 2019-12-11 03:56:43
问题 Hoping someone can give some advice regarding the Producer/Consumer Pattern – in particular on how best to achieve a Queue/BlockingCollection which is COMMON to all producers/consumer class instances? Lets Simplify the scenario; Consider I have; A single Producer class A single single Consumer class. A Service Class which contains instances of both the Producer and Consumer Class. The Service Class simply tells the Producer/Consumer to Start and Stop Work. The producer will populate a

Using a BlockingCollection to queue Tasks

时光总嘲笑我的痴心妄想 提交于 2019-12-11 00:53:16
问题 I am trying to create a way to queue up Tasks to run, so I have tried to implement it using a BlockingCollection . The problem I find is whenever I try to add the Task , the Task executes. Sample code as below: private void button1_Click(object sender, EventArgs e) { textBox2.Clear(); for (int i = 0; i < 10; i++) _processCollection.Add(BigTask(i)); } static BlockingCollection<Task> _processCollection = new BlockingCollection<Task>(); Thread ConsumerThread = new Thread(LaunchConsumer); private

Can't add items to the collection in the second round

天大地大妈咪最大 提交于 2019-12-10 18:15:51
问题 Basically I have a blockingcollection in my windows service application, each time I want to add 4 items to the collection then processing it. The first round is okay, but the second round failed. The error is The BlockingCollection has been marked as complete with regards to additions. My code: public static BlockingCollection<Tuple<ChannelResource, string>> bc = new BlockingCollection<Tuple<ChannelResource, string>>(); public static List<string> list = new List<string>(); // then add 100

Concurrent Collections and Unique elements

六月ゝ 毕业季﹏ 提交于 2019-12-10 14:48:56
问题 I have a concurrent BlockingCollection with repeated elements. How can modify it to add or get distinct elements? 回答1: The default backing store for BlockingCollection is a ConcurrentQueue . As somebody else pointed out, it's rather difficult to add distinct items using that. However, you could create your own collection type that implements IProducerConsumerCollection , and pass that to the BlockingCollection constructor. Imagine a ConcurrentDictionary that contains the keys of the items

How to implement generic callbacks using the C# Task Parallel Library and IProducerConsumerCollection?

心已入冬 提交于 2019-12-08 00:40:47
问题 I have a component that submits requests to a web-based API, but these requests must be throttled so as not to contravene the API's data limits. This means that all requests must pass through a queue to control the rate at which they are submitted, but they can (and should) execute concurrently to achieve maximum throughput. Each request must return some data to the calling code at some point in the future when it completes. I'm struggling to create a nice model to handle the return of data.

The .Net Concurrent BlockingCollection has a memory leak?

佐手、 提交于 2019-12-07 03:50:39
问题 I'm using a Producer/Consumer Pattern with a System.Collection.Concurrent.BlockingCollection<DataTable > to retrieve data from a database (producer) and create a Lucene Index on the data (consumer). The Producer grabs 10000 records at a time and adds the set to the BlockingCollection<DataTable> . The consumer (which is a little bit slower) then grabs those 10000 and creates an index. The blocking collection is bounded to 5 <DataTable> of 10000 rows each. At first the program runs great but

what better do we have to asynchronous execute method than BlockingCollection?

倾然丶 夕夏残阳落幕 提交于 2019-12-06 13:36:03
问题 I wrote such example to measure how fast BlockingCollection for asynchronous execution using System; using System.Collections.Concurrent; using System.Diagnostics; using System.Threading; using System.Threading.Tasks; namespace TestBlockingCollection { class Program { static void Main(string[] args) { BlockingCollection<int> blockingCollection = new BlockingCollection<int>(); Stopwatch sw = Stopwatch.StartNew(); Task.Factory.StartNew(() => { int i = 0; while (true) { Console.WriteLine("Adding