Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should I use LongRunning Task Creation option?

I have an Azure Worker Role that has three types of processes:

  • C# thread that reads from the database and writes to worker-input-queue (Task1)
  • Java thread that reads from worker-input-queue does work and writes to worker-output-queue
  • C# thread that reads from worker-output-queue and writes to database (Task2)

Task1 and Task2 run indefinitely and sleep if their respective queues are empty.

My code looks like this:

SpawnJavaProcesses();  
Task.Factory.StartNew(Task1);  
Task.Factory.StartNew(Task2);  
while(true)  
{  
    //do some trivial sporadic work  
    Thread.Sleep(60*1000);  
}  

My questions:

  • Should I use the LongRunning task creation option when starting Task1 and Task2?
  • Is there a better way to implement what I'm trying to do here?
like image 995
Gilad Gat Avatar asked Nov 29 '12 18:11

Gilad Gat


1 Answers

If you have a few threads that are long running it would be best to use the LongRunning option. By choosing this option you'll be running in a thread outside of the thread pool. This is also something which was explained by Stephen Toub (from the Parallel Extensions team):

It's not a specific length per se. If you're generating a lot of tasks, LongRunning is not appropriate for them. If you're generating one or two tasks that will persist for quite some time relative to the lifetime of your application, then LongRunning is something to consider.

like image 112
Sandrino Di Mattia Avatar answered Oct 31 '22 20:10

Sandrino Di Mattia