I'm currently planning a Windows service. It will be a multi-threaded application which will continuously check for database records and process them. My first thoughts were to set a number of max available threads and create new thread for each process. This works OK but since I'm creating a new thread for every new process I fear that its overhead will multiply if there are lots of records to process. My question is, is this a good design or would you recommend any other solutions?
Here's what the application will do basically:
- Check for available number of threads
- If there are available threads check the database for records to process.
- If there are records to process select top 100 of them
- Create a new process
- Call a web service for each record and update the record according to web service call result (This call+update usually take around 500ms so process will be live for about 50 seconds)
- Continue to step 1
This is currently what I am doing:
timer1.Tick += Tick();
private void Tick()
{
//do some text logging
//do some TextBox updating
}
int MaxThreads = 10
while(true)
{
if(ThreadCount < MaxThreads)
{
new Thread(() => Process()).Start();
ThreadCount++;
}
else
{
Thread.Sleep(10000);
}
}
private void Process()
{
//call ws
//update records
ThreadCount--;
}