WaitHandle.WaitAll 上的 NotSupportedException
我正在尝试执行以下代码。该代码尝试并行下载和保存图像。我传递了要下载的图像列表。我用 C# 3.0 编写了此代码,并使用 .NET Framework 4(VS.NET Express 版本)对其进行了编译。每次我尝试运行程序时,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");
}
您能让我了解我遇到的问题是什么吗?
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否使用
[STAThread]
属性标记了其中一个方法?Did you mark one of the methods with
[STAThread]
attribute?您是否尝试过设置线程的公寓状态?
Have you tried setting the apartment state for the thread?
我建议不要使用多个
WaitHandle
实例来等待完成。请改用 CountdownEvent 类。它会产生更优雅和可扩展的代码。另外,WaitHandle.WaitAll
方法仅支持最多 64 个句柄,并且无法在 STA 线程上调用。通过重构代码以使用规范模式,我想出了这个。I advise against using multiple
WaitHandle
instances to wait for completion. Use the CountdownEvent class instead. It results in more elegant and scalable code. Plus, theWaitHandle.WaitAll
method only supports up to 64 handles and cannot be called on an STA thread. By refactoring your code to use the canonical pattern I came up with this.