I am trying to execute the following code. The code tries to parallely download and save images. I pass a list of images to be downloaded. I wrote this in C# 3.0 and compiled it using .NET Framework 4 (VS.NET express edition). The WaitAll operation is resulting in a NotSupportedException (WaitAlll for multiple handles on a STA thread is not supported) everytime I try to run my program. I tried removing SetMaxThreads
, but that didn't do any difference.
public static void SpawnThreads(List<string> imageList){
imageList = new List<string>(imageList);
ManualResetEvent[] doneEvents = new ManualResetEvent[imageList.Count];
PicDownloader[] picDownloaders = new PicDownloader[imageList.Count];
ThreadPool.SetMaxThreads(MaxThreadCount, MaxThreadCount);
for (int i = 0; i < imageList.Count; i++) {
doneEvents[i] = new ManualResetEvent(false);
PicDownloader p = new PicDownloader(imageList[i], doneEvents[i]);
picDownloaders[i] = p;
ThreadPool.QueueUserWorkItem(p.DoAction);
}
// The following line is resulting in "NotSupportedException"
WaitHandle.WaitAll(doneEvents);
Console.WriteLine("All pics downloaded");
}
Can you please let me understand what is the issue I am running into?
Thank you