I have some code here that works as expected when I install it / run it on my own computer, Windows 7, but when I run it on other servers (2003 and 2008) it does not. The code is from a .NET4 WCF Service Library that I use in a Windows service. Here it is, simpllified.
public void monitorQueueAndDoStuff() {
MonitorRetryQueue();
MonitorMainQueue();
}
private void MonitorMainQueue() {
Log.Info("MonitorMainQueue called");
Task.Factory.StartNew(() =>
{
Log.Info("new thread monitoring queue");
// ...NMS stuff
while (!stopped) {
ITextMessage mess = null;
mess = blockingMessageCollection.Take();
sendToQueue(mess);
}
}
}
});
}
private void MonitorRetryQueue() {
Task.Factory.StartNew(() =>
{
//...NMS stuff
consumer.Listener += new MessageListener(OnRetryErrorMessage);
Log.Info("new thread monitoring second queue");
//need to be constantly up for the consumer to hang around
while (!stopped) {
Thread.Sleep(1000);
}
}
}
});
}
The threads should enter loops to do some work. The main one blocks on a BlockingCollection.
Now, it creates both tasks but it only enters the second, it never prints “new thread monitoring queue” in the log. I cannot grasp why not. I tried Remote Debugging but as it never enters the code I couldn’t see anything of value.
I haven’t found anything that would change the behavior of the code on the deployed server. Anyone here might have a clue? Any settings in the Visual Studio project?
Sometimes this kind of behaviour is an indication of an overloaded
ThreadPool.Seeing as these are long running/blocking tasks, they should not be scheduled to run in the
ThreadPool, which is whereTask.Factory.StartNewwill be sending them using the defaultTaskScheduler.IMO,
Task.Factory.StartNewis probably not best suited to this, and you’d be better off spinning up your own threads to run these loops.