4

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

3 Answers 3

7

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, the WaitHandle.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.

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"); 
} 
1
  • Thanks Brian. Will try that out.
    – rkg
    Sep 24, 2010 at 19:52
5

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

3
  • Thanks Danny! The main application thread is indeed marked as [STAThread]. I removed it and works like magic. Why does VS.NET defaults all the entry methods to [STAThread]?
    – rkg
    Sep 24, 2010 at 6:04
  • @Ravi: I don't think this attribute will be marked to main by default. Maybe it was marked by yourself?
    – Cheng Chen
    Sep 24, 2010 at 6:12
  • I know this is a bit old, but In VS2012 Express for Desktop - I created a brand new application from scratch and indeed in Program.cs file, Main is decorated with the STAThread attribute. I removed it and now it works for me. Thanks guys!
    – Losbear
    May 15, 2013 at 16:28
0

Have you tried setting the apartment state for the thread?

thread.SetApartmentState (System.Threading.Apartmentstate.MTA );
1
  • Thank you, But I am just creating a ThreadPool, not individual threads! ThreadPool does not allow me to set the ApartmentState.
    – rkg
    Sep 24, 2010 at 5:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.