ASP.NET 3.5 webapp should run several tasks that take several hours to complete. For obvious reasons, the pages that run these tasks cannot wait for them to complete, and no one wants to wait a long time for an answer, so tasks must be asynchronous.
There is a helper class to handle all these long-running tasks. The main method that plans and performs these tasks currently looks like this:
public static bool ScheduleTask(TaskDescriptor task, Action action) { bool notAlreadyRunning = TasksAsync.TryAdd(task); if (notAlreadyRunning) { Thread worker = null; worker = new Thread(() => { try { action(); } catch(Exception e) { Log.LogException(e, "Worker"); } TasksAsync.RemoveTask(task); workers.Remove(worker); }); workers.Add(worker); worker.Start(); } return notAlreadyRunning; }
In earlier implementations, we used the ThreadPool.QueueUserWorkItem approach, but the result was always the same: after aprox. 20-30 minutes Thread interruption stops.
Does anyone know why this is happening? or how can it be prevented?
Additional Information:
- Standard IIS configuration.
- Tasks can be any, queries to the database and / or I / O, etc.
UPDATE: Solutions
Thank you all for your answers. Now I do not know what question the answer should be noted. All of them are valid and are possible solutions to this problem. Wait today and mark as the answer to the answer with the most votes, in case of a tie I will choose the first answer shown, as a rule, they are ordered by most relevance.
For those who want to know the solution that I choose, again due to time constraints, there was a change in the configuration of IIS utilization, but what I consider to be the ideal solution based on my research and, of course, the answers below, is to create " Worker Service "and using the communication solution between the ASP.NET App and the new" Worker Service "to coordinate long-term work.
source share