我这里有一些code,当我安装它/我自己的电脑,Windows 7的运行按预期工作,但是当我在其他服务器(2003和2008)运行它没有。在code是一个.NET4 WCF服务库,我在Windows服务中使用。这是,simpllified。
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);
}
}
}
});
}
该线程应该进入循环做一些工作。其中主要的一个模块在一个BlockingCollection。 现在,它会创建两个任务,但只进入第二个,它永远不会打印出新的线程监控队列在日志中。我无法把握,为什么不。我尝试远程调试,但它从未进入code我看不出任何有价值的东西。
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.
我还没有发现任何会改变部署的服务器上的code中的行为。有人在这里可能有一个线索?在Visual Studio项目的任何设置?
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
.
眼看因为这些都是长时间运行/阻塞的任务,他们不应该被安排在线程池
运行,这是其中 Task.Factory。 StartNew
将使用默认将它们发送的TaskScheduler
。
Seeing as these are long running/blocking tasks, they should not be scheduled to run in the ThreadPool
, which is where Task.Factory.StartNew
will be sending them using the default TaskScheduler
.
IMO, Task.Factory.StartNew
可能不是最适合这一点,你会更好旋转起来你自己的线程来运行这些循环。
IMO, Task.Factory.StartNew
is probably not best suited to this, and you'd be better off spinning up your own threads to run these loops.
ThreadStart action=()=>{
//do your thing
};
Thread thread=new Thread(action){IsBackground=true};
thread.Start();