【发布时间】:2010-09-24 05:45:39
【问题描述】:
我正在尝试执行以下代码。该代码尝试并行下载和保存图像。我传递了要下载的图像列表。我用 C# 3.0 编写了它,并使用 .NET Framework 4(VS.NET 快速版)对其进行了编译。每次我尝试运行我的程序时,WaitAll 操作都会导致 NotSupportedException(不支持 STA 线程上的多个句柄的 WaitAllll)。我尝试删除SetMaxThreads,但这没有任何区别。
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");
}
你能告诉我我遇到了什么问题吗?
谢谢
【问题讨论】:
标签: c# multithreading threadpool