Task.Factory.StartNew在部署时不执行任务

ejp*_*jpb 5 c# .net-4.0 visual-studio-2010 task-parallel-library

我有一些代码在我安装它/在我自己的计算机上运行时,按预期工作,Windows 7,但是当我在其他服务器(2003和2008)上运行它时,它没有.代码来自我在Windows服务中使用的.NET4 WCF服务库.这是简单的.

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);
        }
      }
    }
      });
}
Run Code Online (Sandbox Code Playgroud)

线程应该进入循环来做一些工作.BlockingCollection上的主要块.现在,它创建了两个任务,但只进入第二个任务,它从不在日志中打印"新线程监视队列".我不明白为什么不.我尝试了远程调试,但因为它从未输入代码,所以我看不到任何有价值的东西.

我没有找到任何会改变已部署服务器上的代码行为的内容.这里的任何人都可能有线索吗?Visual Studio项目中的任何设置?

spe*_*der 9

有时这种行为表明过载ThreadPool.

看到这些是长时间运行/阻塞任务,它们不应该被安排在运行中ThreadPool,这是Task.Factory.StartNew使用默认值发送它们的地方TaskScheduler.

IMO,Task.Factory.StartNew可能不是最适合这种情况,你最好自己动手来运行这些循环.

ThreadStart action=()=>{
    //do your thing
};
Thread thread=new Thread(action){IsBackground=true};
thread.Start();
Run Code Online (Sandbox Code Playgroud)