I have a service that needs to run forever in order to monitor one server. In order to do so I have 5 tasks that need to be done by the service (5 at the moment, there are going to be hundreds).
I created a threadpool with maximum 5 threads. There is a queue that the threads use in order to get the task, then each thread processes the task and then enqueues it again. The queue contains objects with 5 small attributes.
After the service runs for a while I get an OutOfMemoryException, at the line where I execute this:
ThreadPool.QueueUserWorkItem(currentJob.ProcessJob, (object)timeToWwait);
The timeToWwait is an int and I don't use it anymore.
The context is like this:
ThreadPool.SetMinThreads(0, 0);
ThreadPool.SetMaxThreads(5, 5);
while (true)
{
bool processJob = false;
bool checkedFrontEnd = false;
_qLock.EnterWriteLock();
try
{
if (_jobQueue.Count > 0)
{
currentJob = _jobQueue.Dequeue();
// currentJob.IsActive = true;
// processJob = true;
}
}
finally
{
_qLock.ExitWriteLock();
}
ThreadPool.QueueUserWorkItem(currentJob.ProcessJob, (object)timeToWwait);
//currentJob.ProcessJob((object)timeToWwait);
_qLock.EnterWriteLock();
try
{
_jobQueue.Enqueue(currentJob);
}
finally
{
_qLock.ExitWriteLock();
}
Do you have any suggestions please?