本文介绍了WaitHandle.WaitAll上的NotSupportedException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试执行以下代码.该代码尝试并行下载和保存图像.我传递了要下载的图像列表.我用C#3.0编写了此代码,然后使用.NET Framework 4(VS.NET Express版)对其进行了编译.每当我尝试运行程序时,WaitAll操作都会产生 NotSupportedException(不支持STA线程上多个句柄的WaitAlll).我尝试删除SetMaxThreads,但这没有任何区别.

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?

谢谢

推荐答案

您是否使用[STAThread]属性标记了其中一种方法?

Did you mark one of the methods with [STAThread] attribute?

这篇关于WaitHandle.WaitAll上的NotSupportedException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 05:03