我想执行下面的代码。该代码尝试并行下载并保存图像。我传递要下载的图像列表。我在C#3.0中编写了这个代码,并使用.NET Framework 4(VS.NET express edition)编译它。 WaitAll操作产生了一个NotSupportedException(对于STA线程上的多个句柄的WaitAlll不受支持)每当我尝试运行我的程序时。我试图删除SetMaxThreads
,但这没有任何区别。NotSupportedException在WaitHandle.WaitAll
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");
}
你能让我明白我遇到的问题是什么?
谢谢
谢谢Danny!主应用程序线程确实标记为[STAThread]。我删除它,像魔术般运作。为什么VS.NET将所有的入口方法默认为[STAThread]? – rkg 2010-09-24 06:04:53
@Ravi:我不认为这个属性默认会被标记为'main'。也许它是由你自己标记的? – 2010-09-24 06:12:19
我知道这有点旧了,但是在VS2012 Express for Desktop中 - 我从零开始创建了一个全新的应用程序,实际上在Program.cs文件中,Main用STAThread属性进行了装饰。我删除它,现在它适用于我。多谢你们! – Losbear 2013-05-15 16:28:54