Task.Factory.StartNew does not complete the task during deployment

I have code here that works as expected when I install and run it on my own computer, Windows 7, but when I run it on other servers (2003 and 2008), it is not. The code is from the .NET4 WCF Service Library, which I use in a Windows service. Here it is simplified.

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); } } } }); } 

Threads must introduce loops to do some work. The main block is blocked on the BlockingCollection. Now it creates both tasks, but it only enters the second, it never prints a β€œnew thread monitoring queue” in the log. I can’t understand why not. I tried remote debugging, but since it never goes into code, I could not see anything.

I did not find anything that could change the behavior of the code on the deployed server. Can anyone here have a clue? Any settings in a Visual Studio project?

+4
source share
3 answers

Sometimes this behavior is a sign of an overloaded ThreadPool .

Seeing that these are long running / blocking tasks, they cannot be launched in ThreadPool , where Task.Factory.StartNew will send them using the default TaskScheduler .

IMO, Task.Factory.StartNew is probably not suitable for this, and you'd better unscrew your own threads to run these loops.

 ThreadStart action=()=>{ //do your thing }; Thread thread=new Thread(action){IsBackground=true}; thread.Start(); 
+8
source

Will any journal messages be printed in the journal? Do you see "MonitorMainQueue called" typing? How do you know that the second task has been launched, but not the first? Could there be a permission issue when creating / writing to the log file?

Edit: Also, in response to what @spender said about long tasks, there is overload to run the task with this option.

Task.Factory.StartNew(MonitorMainQueue, TaskCreationOptions.LongRunning);

+1
source

I had the same problem when deploying in my project.

The problem is with the identifier used by the application pool.

The default is applicationPoolIdentity, which has limited rights.

We changed to "Network Service" and it worked.

Nota: I'm not saying that using NetWorkService is the best solution, but it causes a security problem.

+1
source

All Articles