2009-08-13 97 views
0

我snaged本次从网上递归搜索所有包括subdirectoires问题搜索子目录

的文件如果我指出这一点子大面积(即我的文档或C :)我得到一个错误:

The CLR has been unable to transition from COM context 0x1f6c48 to COM context 0x1f6db8 for 60 seconds. The thread that owns the destination context/apartment is most likely either doing a non pumping wait or processing a very long running operation without pumping Windows messages. This situation generally has a negative performance impact and may even lead to the application becoming non responsive or memory usage accumulating continually over time. To avoid this problem, all single threaded apartment (STA) threads should use pumping wait primitives (such as CoWaitForMultipleHandles) and routinely pump messages during long running operations.

下面是代码 (我相信它,因为个子自称)

void DirSearch(string sDir) 
{ 
    try 
    { 
     foreach (string d in Directory.GetDirectories(sDir)) 
     { 
      foreach (string f in Directory.GetFiles(d)) 
      { 
       string hash = GetMD5HashFromFile(f); 
       Dic_Files.Add(f, hash); 
      } 
      DirSearch(d); 
     } 
    } 
    catch (System.Exception excpt) 
    { 
     Console.WriteLine(excpt.Message); 
    } 
} 
+0

看起来像一个相当无害的目录扫描例程。如果可行的话,您可以发布异常的调用堆栈以及GetMD5HashFromFile()的实现吗? – 2009-08-13 17:47:57

回答

0

调试问题。根据

http://social.msdn.microsoft.com/forums/en-US/vsdebug/thread/ed6db6c8-3cdc-4a23-ab0a-2f9b32470d35/

你们看到的是“托管调试助手”一个(MDA),并且可以通过调试 - >例外禁用...>展开MDA节点,并取消对contextswitchdeadlock箱。

+0

确实能解决问题吗?还是调试问题意味着它只有一个问题,而我正在调试? – Crash893 2009-08-15 13:37:44

+0

代码loks正确。请参阅http://groups.google.com/group/microsoft.public.vsnet.debugging/browse_thread/thread/1e4ac1010ae5920d/03ca1c82ea79287b?pli=1 这是关于尝试检测多线程系统中的死锁的调试器问题。 – 2009-08-15 18:08:35

0

尝试运行的代码在一个单独的线程,这样你就不会阻止你的UI在搜索期间。 BackgroundWorker是最简单的方法。

0

代替使用一个递归函数的,简单地使用Directory.GetFiles

Directory.GetFiles(sdir, null, SearchOption.AllDirectories); 
+0

这是更简单的代码,很好的电话。但是,他遇到的问题是搜索本身耗时太长,需要在单独的线程上完成。 – 2009-08-13 17:48:46

+0

我看到这个选项,但它崩溃,如果一个目录出错,所以它真的不实际 – Crash893 2009-08-15 13:36:50

1

GetFiles方法具有一个覆盖,其允许递归搜索超载之一。我会尝试使用,看看你的问题是否消失...

void DirSearch(string sDir) 
{ 
    try 
    { 
     var files = System.IO.Directory.GetFiles(sDir, "*.*", SearchOption.AllDirectories); 
     foreach (string f in files) 
     { 
      string hash = GetMD5HashFromFile(f); 
      Dic_Files.Add(f, hash); 
     } 
    } 
    catch (System.Exception excpt) 
    { 
     Console.WriteLine(excpt.Message); 
    } 
} 
+0

我看到这个选项,但它崩溃,如果一个目录出错,所以它真的不实际 – Crash893 2009-08-15 13:35:32