NotSupportedException em WaitHandle.WaitAll
Estou tentando executar o seguinte código. O código tenta baixar e salvar imagens em paralelo. Passo uma lista de imagens para serem baixadas. Eu escrevi isso em C # 3.0 e compilei usando o .NET Framework 4 (edição expressa do VS.NET). A operação WaitAll está resultando em umNotSupportedException (WaitAlll para vários identificadores em um encadeamento STA não é suportado) toda vez que tento executar meu programa. Eu tentei removerSetMaxThreads
, mas isso não fez nenhuma diferença.
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");
}
Por favor, deixe-me entender qual é o problema que estou enfrentando?
Obrigado