4

Is it possible to safely enumerate a ShellContainer in a background thread using Windows API CodePack?

I'm trying to enumerate ShellFolders (Desktop, My Computer, etc) and everything works great until I hit something that blocks the UI. In particular, 'Network' takes about 40 seconds to return a list of children.

I tried to run in a background Task, and it appears to work, but then some folders with known subfolders start to fail and return items with no name or parsing name. It seems that the interop has broken at that point and makes me think it is not thread safe.

new Task(() =>
{
   // if this fails, the items will be ShellObjects 
   // but will have no parsingnames
   var items = MyShellFolder.ToArray();
   dispatcher.Invoke(() => MyChildren.AddRange(items));
}).Start();
bembleton
  • 51
  • 5
  • 3
    Shell folders should only be enumerated from an STA thread. A task is always MTA. A call to Thread.SetApartmentState() is required before starting the thread. – Hans Passant Nov 22 '13 at 15:56
  • Indeed, thank you. I switched to a STA thread which resolved the access issues. – bembleton Nov 22 '13 at 21:02

0 Answers0