I think your best bet would be to start off with something simple which will allow you to understand the performance characteristics of your problem.
List<string> items = GetListOfPdfFilesToProcess();
int numCores = 4;
int maxListChunkSize = (int)Math.Ceiling(items.Count / (double)numCores);
ManualResetEvent[] events = new ManualResetEvent[numCores];
for (int i = 0; i < numCores; i++)
{
ThreadPool.QueueUserWorkItem(ProcessFiles, new object[]
{
items.Skip(i * maxListChunkSize).Take(maxListChunkSize).ToList(), events[i]
});
}
WaitHandle.WaitAll(events);
....
private static void ProcessFiles(object state)
{
object[] stateArray = (object[])state;
List<string> filePaths = (List<string>)stateArray[0];
ManualResetEvent completeEvent = (ManualResetEvent)stateArray[1];
for (int i = 0; i < filePaths.Count; i++)
{
csCommon.pdfFilesCompressAndMove(your parameters);
}
completeEvent.Set();
}
The main thing here is to split the work up into numCores
chunks. This way you should be able to make good use of all CPU cores but keep a pretty simple programming model.
Keep in mind that this does not do any error handling - you'll want to take care of that. It might also pay to put some thought into what to do if csCommon.pdfFilesCompressAndMove
fails to process a file. The simplest approach would be to log the error and inspect it later, though you could attempt to reprocess the file if you felt it would succeed the next time.
You'll notice that the state
object is just an array; if you need to pass in lots of parameters to ProcessFiles
then it may be simpler to wrap those parameters up into a single object and pass that in as the state
.
Edit:
To use from your Tick
event:
private void TimerTick(object sender, EventArgs e)
{
//Disabling the timer will ensure the `TimerTick` method will not try to run
//while we are processing the files. This covers the case where processing takes
//longer than 2 minutes.
timer.Enabled = false;
//Run the first block of code in my answer.
//Reenabling the timer will start the polling back up.
timer.Enabled = true;
}
I would also recommend checking the number of files you have to process: if there are none, reenable the timer and return. This will avoid queuing up a bunch of operations that don't actually do anything.