如何实时向后台工作人员报告progresschanged事件的目录数量?

时间:2016-03-19 21:31:54

标签: c# .net winforms

在这两种方法中GetDirectories和MyGetDirectories我正在递归所有目录和子目录。我想在progresschanged事件中的label2上显示,就像一个计算目录数量直到完成的计数器。

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方法中工作。所以我被困在这里。

2 个答案:

答案 0 :(得分:0)

确保将以下内容设为true:

_FileInformationWorker.WorkerReportsProgress = true;

还要确保以异步方式启动工作,如下所示:

_FileInformationWorker.RunWorkerAsync();

This示例也可以帮助您完成任务。

答案 1 :(得分: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>();
            }
        }
    }
}