I have a loop that I don’t want to continue until LoadAmazonDataByBatch() has returned. I know there must be a straight forward way of doing it, and I’m almost certain I’m approaching the problem wrong.
const int batchSize = 500;
for (int i = 0; i < total; i = i + batchSize)
{
LoadAmazonDataByBatch(i, batchSize, fileList, total, amazonLogHandler, stopWatch);
}
LoadAmazonDataByBatch() does a bunch of things on worker threads including creating a temporary DataSet that would get very large without the batching. I don’t want to create a new DataSet until the old one is processed and disposed (by LoadAmazonDataByBatch).
Obviously the way this is written now everything happens almost all at once.
How can I approach this better?
You need to do some sort of thread synchronization.
Not clear where you got the
LoadAmazonByBatch(), but I’d suggestchecking the doc for that function to see if there is a synchronous version of the operation.
if no doc is available, then you will need to roll up yr sleeves. It may require viewing or modifying the source of
LoadAmazonByBatch(). Look for aManualResetEventthat is set by the workers when they are finished. Or, maybe there is a regular .NET event that is emitted by that method when it completes. If those things don’t exist you’ll need to add something like that.