I have daily reports I'm trying to collect en masse from a remote web service my code looks like this:
public static void ProcessEnMasse(System.DateTime fromDate, DateTime endDate)
{
System.Threading.ThreadPool.SetMaxThreads(10, 10);
for (System.DateTime d = fromDate; d <= endDate; d = d.AddDays(1))
{
System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(day => ProcessOneDay(d)));
}
}
public static void ProcessOneDay(System.DateTime theDate)
{
Log.Debug(string.Format("Processing {0:yyyy-MM-dd}...", theDate));
var thePackager = new DataPackager();
thePackager.CreateDatabaseImportPackage(theDate, theDate, true, false);
}
... when I look at the logs, I notice that several of the threads are processing the same date. Why is this, and what do I need to do to prevent that from happening?