I am reading data via ZMQ and executing tasks in a separate thread based on their topic. I noticed that when the data frequency is really high (~1 message every 1ms), some of the tasks take a really long time to execute.
This is basically what I'm doing :
while(true){
item = zmqSubscriber.ReceiveData(out topic, out ConsumeErrorMsg);
if (topic.Equals(topic1))
{
Task.Run(() => ExecuteTask1(item));
}
else if (topic.Equals(topic2)
{
Task.Run(() => ExecuteTask2(item));
}
else if (topic.Equals(topic3))
{
Task.Run(() => ExecuteTask3(item));
}
}
When the data frequency is a bit lower (10 messages every 100ms), I did not notice any behavior issues.
I'm new to C# and I'm wondering if this could be due to the maximum number of active thread pool threads being low. I read here that the number could be increased, however, it's not a good practice: ThreadPool.SetMinThreads(Int32, Int32) Method
So I was wondering if there was a better way of achieving what I'm trying to do??