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?
Task. Run(action) internally uses the default TaskScheduler , which means it always offloads a task to the thread pool. StartNew(action) , on the other hand, uses the scheduler of the current thread which may not use thread pool at all!
StartNew(Action<Object>, Object, CancellationToken, TaskCreationOptions, TaskScheduler) Creates and starts a task for the specified action delegate, state, cancellation token, creation options and task scheduler.
To start a task in C#, follow any of the below given ways. Use a delegate to start a task. Task t = new Task(delegate { PrintMessage(); }); t. Start();
Differences Between Task And ThreadThe Thread class is used for creating and manipulating a thread in Windows. A Task represents some asynchronous operation and is part of the Task Parallel Library, a set of APIs for running tasks asynchronously and in parallel. The task can return a result.
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 where Task.Factory.StartNew
will be sending them using the default TaskScheduler
.
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();
Do any log messages get printed in the log? Do you see "MonitorMainQueue called"
get printed? How do you know the second Task is started but not the first? Could it be a permission issue with creating/writing to the log file?
Edit: Additionally, in response to what @spender said about long running tasks, there is an overload to start the task with that option.
Task.Factory.StartNew(MonitorMainQueue, TaskCreationOptions.LongRunning);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With