2016-03-19 29 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; 現在它工作。 –

相關問題