Hi I have a short question regarding tasks. As far as I understand Tasks can start multiple threads in itself.
Lets say I have two hardware sensors which give me data over two different dataports.
I want to implement them as producers in my c# project and then do something with the data. Would it make sense to start the data collection in two different tasks? Or should i implement them in the same task since c# will automatically put them on different threads?
var producerWorker = Task.Factory.StartNew(() => SensorB(number));
var producerWorker2 = Task.Factory.StartNew(() => SensorA(number));
or
var producerWorker = Task.Factory.StartNew(() => Sensor_A_AND_B(number));
My second problem is: When I have two different producers in two different tasks, how do I add their data to the same BlockingCollection queue if they have different datatypes but need to be at the same position in the queue?
For example if I have queueA for SensorA, queueB for SensorB, and queueC. Both queues can be filled at different speeds. So lets say queueA has 50 elements, but SensorB is a lot faster and already has 100 elements stored in queueB. However I need to retrieve the data in a way, so that I can place queueA[33].data and queueB[33].data in queueC[33].data. Of course I would not like to start with element33, but always with the first element which was stored in queueA and queueB....
I hope you get what i mena
Tasks are executed in whatever way the runtime thinks is the best. Generally, there's a thread pool and both tasks run on available threads. If you really need to poll two sensors in parallel, I would recommend you to use two real threads to poll and use Reactive Extensions to process the readings in sync.