2016-03-19 27 views
0

在这两个方法GetDirectories和MyGetDirectories我得到递归所有的目录和子目录。我想在progresschanged事件中的label2上显示一个计数器,它可以计算目录的数量直到完成。如何实时报告backgroundworker progresschanged事件的目录数量?

private void _FileInformationWorker_DoWork(object sender, DoWorkEventArgs e) 
     { 
      MySubDirectories = GetDirectories(BasePath).ToArray(); 
     } 

     private void _FileInformationWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) 
     { 

     } 

然后GetDirectories方法

private List<DirectoryInfo> GetDirectories(string basePath) 
     { 
      IEnumerable<string> str = MyGetDirectories(basePath); 

      List<DirectoryInfo> l = new List<DirectoryInfo>(); 
      l.Add(new DirectoryInfo(basePath)); 

      IEnumerable<DirectoryInfo> dirs = str.Select(a => new DirectoryInfo(a)); 
      l.AddRange(dirs); 

      return l; 
     } 

而且方法MyGetDirectories

private static IEnumerable<string> MyGetDirectories(string basePath) 
     { 
      try 
      { 
       string[] dirs = Directory.GetDirectories(basePath); 
       return dirs.Union(dirs.SelectMany(dir => MyGetDirectories(dir))); 
      } 
      catch (UnauthorizedAccessException) 
      { 
       return Enumerable.Empty<string>(); 
      } 
     } 

我试图在方法MyGetDirectories我添加了一个计数器称为变量countDirectories和我检查它计数。

static int countDirectories = 0; 
     private static IEnumerable<string> MyGetDirectories(string basePath) 
     { 
      try 
      { 
       string[] dirs = Directory.GetDirectories(basePath); 
       countDirectories = countDirectories + dirs.Length; 
       return dirs.Union(dirs.SelectMany(dir => MyGetDirectories(dir))); 
      } 
      catch (UnauthorizedAccessException) 
      { 
       return Enumerable.Empty<string>(); 
      } 
     } 

然后在DoWork的事件中,我所做的:

private void _FileInformationWorker_DoWork(object sender, DoWorkEventArgs e) 
     { 
      MySubDirectories = GetDirectories(BasePath).ToArray(); 
      _FileInformationWorker.ReportProgress(0,countDirectories.ToString()); 
     } 

而在progresschanged事件

private void _FileInformationWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) 
     { 
      label2.Text = e.UserState.ToString(); 
     } 

但它不给LABEL2任何报告我想,因为它仍在里面工作GetDirectories方法。所以我卡在这里。

回答

0

这是仅按钮和标签形式的代码就可以了:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.IO; 

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     DirectoryInfo[] MySubDirs; 
     BackgroundWorker w; 
     int count; 

     private void button1_Click(object sender, EventArgs e) 
     { 
      button1.Enabled = false; 
      count = 0; 
      w = new BackgroundWorker(); 
      w.DoWork += w_DoWork; 
      w.ProgressChanged += w_ProgressChanged; 
      w.WorkerReportsProgress = true; 
      w.RunWorkerCompleted += w_RunWorkerCompleted; 
      w.RunWorkerAsync(); 

     } 

     void w_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) 
     { 
      button1.Enabled = true; 
     } 

     void w_ProgressChanged(object sender, ProgressChangedEventArgs e) 
     { 
      label1.Text = e.UserState.ToString(); 
     } 

     void w_DoWork(object sender, DoWorkEventArgs e) 
     { 
      MySubDirs = GetDirectories("d:\\prj").ToArray(); 
     } 

     private List<DirectoryInfo> GetDirectories(string basePath) 
     { 
      IEnumerable<string> str = MyGetDirectories(basePath); 

      List<DirectoryInfo> l = new List<DirectoryInfo>(); 
      l.Add(new DirectoryInfo(basePath)); 

      IEnumerable<DirectoryInfo> dirs = str.Select(a => new DirectoryInfo(a)); 
      l.AddRange(dirs); 

      return l; 
     } 
     //not static so we can report progress from it 
     private IEnumerable<string> MyGetDirectories(string basePath) 
     { 
      try 
      { 
       string[] dirs = Directory.GetDirectories(basePath); 
       count += dirs.Length; 
       w.ReportProgress(0, count.ToString()); 
       return dirs.Union(dirs.SelectMany(dir => MyGetDirectories(dir))); 
      } 
      catch (UnauthorizedAccessException) 
      { 
       return Enumerable.Empty<string>(); 
      } 
     } 
    } 
} 
+0

shibormot谢谢我将它标记为答案。我还有一个问题,但不知道是否要为它提出一个新问题。目录的数量报告正在工作。但是如果我想实时将自己的目录报告给变量MySubDirs呢?我的意思是现在在你的代码和我的代码中,我们需要等待获取目录完成,然后MySubDirs不是空的,并有内部的目录。但是因为我正在使用另一个背景工作者来处理MySubDirs。 –

+0

也许以某种方式报告并填写MysubDirs与我已经目录。我正在做的是循环在另一个背景工作者的MySubDirs中的目录并搜索MySubDirs中的这个目录。我想知道如何在第一个背景工作者完成这个过程之前开始第二个backjgroundworker和MySubDirs的工作。 –

+0

最好问一个新问题,因为它不会是一个简短的回答 – shibormot

0

确保您已设置以下为true:

_FileInformationWorker.WorkerReportsProgress = true; 

另外,还要确保你异步启动工作如下:

_FileInformationWorker.RunWorkerAsync(); 

This例子来帮助你完成你的任务,以及。

+0

我将它设置了。解决方法是将ReportProgress行:_FileInformationWorker.ReportProgress移动到更新计数器的行后面的MyGetDirectories方法内部:countDirectories = countDirectories + dirs.Length; 现在它工作。 –

相关问题