将所有文件记录在一个大目录中
我有许多目录,其中包含大量文件(约 10,000 个)。 我想在我的应用程序中创建这些文件的列表,并且已经对 io 访问进行了线程化,以便应用程序在加载时不会冻结。 但是,如果我在加载所有文件之前退出应用程序,则线程不会响应 .Join(),直到完成对 dirInfo.GetFiles(...) 的调用:
// ... mythread
DirectoryInfo dirInfo = new DirectoryInfo(path);
foreach(FileINfo file in dirInfo.GetFiles(extension))
{
// with large directories, the GetFiles call above
// can stall for a long time
...
将文件缓存到 foreach 之外只会解决问题。 我需要某种线程式回调方式来查找目录中的文件,但我不知道该怎么做。 任何帮助,将不胜感激。
非常感谢, 坦普恩。
I've got a number of directories with a large number of files in them (~10,000). I want to create a list of these files in my app, and have already threaded the io access so that the app doesn't freeze while they load. However if I exit the app before all files are loaded, the thread doesn't respond to the .Join() until the call to dirInfo.GetFiles(...) is completed:
// ... mythread
DirectoryInfo dirInfo = new DirectoryInfo(path);
foreach(FileINfo file in dirInfo.GetFiles(extension))
{
// with large directories, the GetFiles call above
// can stall for a long time
...
Caching the files out of the foreach just moves the problem. I need some kind of threaded, callback-y way of finding the files in the directory and I'm not sure how to do that. Any help would be appreciated.
Many thanks,
tenpn.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该使用 ThreadPool 类中的线程。 这将使其成为后台线程,并且当应用程序关闭时它应该收到 ThreadInteruptException。
You should use a Thread from the ThreadPool class. This will make it a background thread and it should receive a ThreadInteruptException when the application closes.
当您的应用程序即将执行时,您可以调用 Thread.Abort()关闭(在加入之前)。
另外,您可能希望在线程方法中捕获 ThreadAbortException 并在必要时进行一些终结/清理。
You can call Thread.Abort() when your application is about to close (before the Join).
Also, you might want to catch the
ThreadAbortException
in the thread method and do some finalization/cleaning, if necessary.