如何在完成之前访问 DirectoryInfo.EnumerateFiles [英] How to access DirectoryInfo.EnumerateFiles before it has completed

查看:32
本文介绍了如何在完成之前访问 DirectoryInfo.EnumerateFiles的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我问的问题中检索一个快速列出文件夹和所有子文件夹中的文件名 以及我发现的其他一些文件,搜索许多文件的方法似乎是使用 EnumerateFiles 方法.

In the question I asked Retrieve a list of filenames in folder and all subfolders quickly And a few others I have found, it seems the way to search many files is to use the EnumerateFiles Method.

EnumerateFiles 和 GetFiles 方法的区别如下:使用EnumerateFiles,就可以开始枚举名字的集合了在返回整个集合之前;当您使用 GetFiles 时,您必须等待整个名称数组返回,然后才能访问数组.因此,当您处理许多文件并且目录,EnumerateFiles 可以更高效.

The EnumerateFiles and GetFiles methods differ as follows: When you use EnumerateFiles, you can start enumerating the collection of names before the whole collection is returned; when you use GetFiles, you must wait for the whole array of names to be returned before you can access the array. Therefore, when you are working with many files and directories, EnumerateFiles can be more efficient.

这对我来说听起来很棒,我的搜索大约需要 10 秒钟,所以我可以在信息进入时开始制作我的列表.但我无法弄清楚.当我运行 EnumerateFiles 方法时,应用程序会冻结,直到它完成.我可以在后台工作者中运行它,但同样的事情会发生在那个线程上.有什么帮助吗?

This sounds Great for me, my search is taking about 10 seconds, so I can start makign my list as the information comes in. But I can't figure it out. When I run the EnumerateFiles Method, the application freezes until it completes. I could run it in a background worker, but the same thing will happen to that thread. Any help?

 DirectoryInfo dir = new DirectoryInfo(MainFolder);
 List<FileInfo> matches = new List<FileInfo>(dir.EnumerateFiles("*.docx",SearchOption.AllDirectories));

//This wont fire until after the entire collection is complete
DoSoemthingWhileWaiting();

推荐答案

您可以通过将其推送到后台任务来实现.

You can do this by pushing it into a background task.

例如,您可以这样做:

var fileTask = Task.Factory.StartNew( () =>
{
    DirectoryInfo dir = new DirectoryInfo(MainFolder);
    return new List<FileInfo>(
           dir.EnumerateFiles("*.docx",SearchOption.AllDirectories)
           .Take(200) // In previous question, you mentioned only wanting 200 items
       );
};

// To process items:
fileTask.ContinueWith( t =>
{
     List<FileInfo> files = t.Result;

     // Use the results...
     foreach(var file in files)
     {
         this.listBox.Add(file); // Whatever you want here...
     }
}, TaskScheduler.FromCurrentSynchronizationContext()); // Make sure this runs on the UI thread

DoSomethingWhileWaiting();

<小时>

您在评论中提到:


You mentioned in a comment:

我想在列表中显示它们.并在它们进入时完美地将它们发送到主用户界面

I want to display them in a list. and perfect send them to the main ui as they come in

在这种情况下,您必须在后台处理它们,并在它们进入时将它们添加到列表中.例如:

In this case, you'd have to process them in the background, and add them to the list as they come in. Something like:

Task.Factory.StartNew( () =>
{
    DirectoryInfo dir = new DirectoryInfo(MainFolder);
    foreach(var tmp in dir.EnumerateFiles("*.docx",SearchOption.AllDirectories).Take(200))
    {
        string file = tmp; // Handle closure issue

        // You may want to do this in batches of >1 item...
        this.BeginInvoke( new Action(() =>
        {
             this.listBox.Add(file);
        }));
    }
});
DoSomethingWhileWaiting();

这篇关于如何在完成之前访问 DirectoryInfo.EnumerateFiles的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆