Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have the following requirement.
A website in which any number of user can login and click on button. The task inside that button click will be a long running process (May take hours or days to finish the task). So I thought of writing the task in a windows service. So the users can log out from the website, but the task will run in background. But my doubt is how we can handle ‘n’ number of users in this case?
My current solution:-
I have created 4-5 service instances and it will allocate to the first 4 users logged into the website. Rest of the users will be in queue. Once any service is free, then it will allocate to the users who are in queue. But I am not sure this is a correct approach. Because I have used multiple instance of the windows service and written all my code in ‘Onstart’ and ‘Onstop’ event of windows service. (The background process and managing queue task)
I have read like it is not good practice to keep long running process in ‘onstart’ event of windows service as the service may stop after a particular amount of time. So the solution is to use threads to handle the request. My next doubt is shown below.

1) Is it okay if I create a new thread for handling each user request [Means only one service instance, but multiple threads created in that service to handle each user request]? If so how can I call the service again for next user from the website? Because the service is already in running stage, but to allocate a new thread to the user, I should call the service and execute its onstart event. Note that I am also passing some argument to the windows service to identify the task and logged in user.

2) If I handle this in thread, whenever the service stops, will it stop my thread as well (created inside service)? Because I don’t want to do that. I need the thread to be running until it finishes the task.
Posted

1 solution

1) I would not recommend a thread per user. It may create uncontrolled number of thread. Services work the best when number of threads is fixed (say, depends on some configuration). You can always server user requests in a queue. An important hint: you always need a separate thread listening for new connections, so the thread writing/reading to/from network stream would be separate from that.

2) Not really a question. Stopping the service means end of the process lifetime. When you restart the service, this is a new instance of the process. Keep it running at all times.

—SA
 
Share this answer
 
Comments
AmruthaNair 19-Mar-14 1:31am    
Hi,

I have read about the TPL recently. Is TPL is a good option for long running threads?
Sergey Alexandrovich Kryukov 19-Mar-14 1:45am    
TPL is merely built on top of threads; it is abstracted from actual thread, so using TPL you don't necessarily know in what thread some code is executed. I don't think it should be used for long running thread, at least, not routinely.
—SA
AmruthaNair 6-May-14 22:51pm    
Since TPL is imlemented using thread pool, Do I need to use a queue for saving the server request?
Sergey Alexandrovich Kryukov 6-May-14 23:19pm    
Do you mean queue before distributing a request to a thread servicing it? This is a good idea in many cases. Thread pool does not change much, it also have limited number of actual threads. I just advise having some fixed small number of threads.
TPL is the abstraction where you don't deal with threads directly, also can be considered. So, why using a queue? If you have certain number of threads, what should you do if they are all busy with previous request, what should you do if you have one more request? add one more thread (from a thread pool or not)? It would be a bad idea, because the situation can go out of control. No, you should make some requests wait in the queue. Even if you have many load-balanced server hosts, such situation can happen.
—SA
AmruthaNair 7-May-14 0:23am    
Thanks for the comment. I am new to the TPL concept. I have only delt with threads so far. First I thought of using a fixed number of threads , say 10.. and if 11th request come, i will add the request in a queue in db.. when ever a thread is free, i will allocate the thread with new request.
my doubt was, do i need this external queue for saving the requests, will TPL take care of it..

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