0

Let me briefly explain my problem. I want to read data from a sensor with my c# solution. In order to do that I use an event, which pulls the data at a very fast rate from the sensor. The data is then saved to a database in sql.

To achieve that and maximize performance, I registered the event in Task A, which now frequently polls the Data from my sensor, lets say 1000 samples per second to give you an idea.

The data is saved in a blocking collection object with a queue.

What i want to do now is to stark a second task, which saves my data to an sql database, but only if there are more than 5000 samples in my blocking collection queue. How do i achieve that?

I tried to start the second task in my event which runs on the first task, but came over a few problems.

A) Sometimes the second task would not start in my event B) I got an exception that the second task is still runnig (Because the event triggered so fast and it was not done i guess)

Is there any good way to do this?

Best regards

DerBenutzer
  • 311
  • 1
  • 5
  • 20
  • Could you have the second task check the number of samples in the queue, and if over 5,000, store it in a local tmp object/queue/block/whatever, then iterate through and insert the samples into the database, deleting the 5,000 from the original main queue to avoid duplicates? – Corey Thompson Feb 04 '16 at 23:38
  • Or to expand on Corey's suggestion, atomically (or as close as you can get, maybe with a mutex) swap the queue with an empty one, so that you are blocking access to it in Task A for the minimal amount of time? – object88 Feb 05 '16 at 03:11

0 Answers0