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.