rkg*_*rkg 4 c# multithreading threadpool
我正在尝试执行以下代码.代码尝试并行下载和保存图像.我传递了要下载的图像列表.我在C#3.0中编写了这个,并使用.NET Framework 4(VS.NET express edition)进行编译.每次我尝试运行我的程序时,WaitAll操作都会导致NotSupportedException(不支持STA线程上的多个句柄的WaitAlll).我尝试删除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");
}
Run Code Online (Sandbox Code Playgroud)
能否让我了解我遇到的问题是什么?
谢谢
我建议不要使用多个WaitHandle实例来等待完成.请改用CountdownEvent类.它产生更优雅和可扩展的代码.此外,该WaitHandle.WaitAll方法仅支持最多64个句柄,并且无法在STA线程上调用.通过重构代码以使用规范模式,我提出了这个问题.
public static void SpawnThreads(List<string> imageList)
{
imageList = new List<string>(imageList);
var finished = new CountdownEvent(1);
var picDownloaders = new PicDownloader[imageList.Count];
ThreadPool.SetMaxThreads(MaxThreadCount, MaxThreadCount);
for (int i = 0; i < imageList.Count; i++)
{
finished.AddCount();
PicDownloader p = new PicDownloader(imageList[i]);
picDownloaders[i] = p;
ThreadPool.QueueUserWorkItem(
(state) =>
{
try
{
p.DoAction
}
finally
{
finished.Signal();
}
});
}
finished.Signal();
finished.Wait();
Console.WriteLine("All pics downloaded");
}
Run Code Online (Sandbox Code Playgroud)