I have a windows service that has a lot of work to do simultaneously. I've looked into threading and found the ThreadPool class. I'm currently stuck, it doesn't seem to have any effect, it's like whatever I'm queuing is never run or called. In the service's OnStart() event I create a thread like this:
Thread mainThread = new Thread(ReceiveMessages);
mainThread.Start();
Inside the method ReceiveMessages() I have a routine that checks a message queue and then iterates through the messages. For each iteration I call the following code to do some work with each message:
ThreadPool.QueueUserWorkItem(new WaitCallback(delegate(object state)
{
Interpreter.InsertMessage(encoding.GetBytes(MessageBody));
}), null);
I think the syntax is right, it compiles with no issues, but I can't help but feel that I am missing something. When I run the service, nothing happens. However, if I replace the above code snippet with this:
insertThread = new Thread(delegate() { Interpreter.InsertMessage(encoding.GetBytes(MessageBody)); });
insertThread .Start();
It works 100%. It's not very efficient though and can cause the service to crash (which is does occasionally, the reason why I'm trying to use TheadPool instead). Can anyone shed some light on the subject?