Click here to Skip to main content
15,887,822 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
Hi there...

Consider the following situation:

1- I have a webservice application. The webservice accept jobs.
2- The jobs are stored into a table.
3- There should be one process/one thread that will process the jobs one by one.

the following is how did I do it:

1- a call for a job is placed
2- the webservice insert the job into the queue
3- the webservice checks the job-thread and if it is not there, it creates it.
4- the thread enters an infinite loop, it sleeps for 10 seconds, and then checks again if there is any job. If so, process the job, and remove it from the table.

the problem is:

when the session of the user terminates , the job-thread is terminated. I want to create a thread/process in the webservice that will never terminate.

any idea?

thanks.
Posted
Comments
Sergey Alexandrovich Kryukov 17-Apr-11 12:16pm    
A very reasonable request and reasonable design; you only need to know more about thread synchronization and usage patterns. I vote my 5 for the Question due to your thinking in right direction.
--SA

1 solution

Yes. I have a complete solution for you.

You should run you thread forever but keep in in wait state until it is awaken for a job to do.
The way to get to such a wait state is using System.Threading.EvenWaitHandle.WaitOne. At the call to this method, your thread is switched off by OS and never scheduled back until awaken, so it consumes zero CPU time. The thread can be awaken by a call in the other thread: System.Threading.Thread.Abort or System.Threading.EvenWaitHandle.Set on the same instance of EvenWaitHandle, that should be done when the task is ready. If your Wait was called with timeout, the thread can be awaken by the expiration of the timeout as well.

But you don't have to program this manually. You need to use a blocking queue. The elements of the queue could be data sets for you tasks or even delegates (see me reference below).

I provide a generic blocking queue in my Tips/Tricks article: Simple Blocking Queue for Thread Communication and Inter-thread Invocation[^]. It is complete with full source code and usage samples (some are nearly ready for your service). Also, pay attention to the alternative on the same page: there is a Framework class doing the same thing in v.4.0. You need to follow the usage I've shown in my samples, though.

—SA
 
Share this answer
 
Comments
Albin Abel 17-Apr-11 13:31pm    
My 5
Sergey Alexandrovich Kryukov 17-Apr-11 13:44pm    
Thank you, Albin.
--SA
Member 7742860 17-Apr-11 14:16pm    
Hi there...

Thanks for the info. I checked that, and while checking some other similar problems I read that when the reference to the thread is lost, the thread is terminated when garbage collection cleans memory. All I had to do was to place a reference to the thread object in the System.Web.HttpApplicationState which solves this issue. I am testing this now, and it is working just fine. I will modify the code to use your technique as well.

thanks for the answer :)
Sergey Alexandrovich Kryukov 17-Apr-11 14:23pm    
You are welcome.
I really strongly advice this pattern as it is proven to be successful in very many scenarios.
Thanks for accepting this Solution.

Good luck, call again,
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900