我怎样才能实时向后工作者报告progresschanged事件的目录数量?(How can i report to the backgroundworker progresschanged event

编程入门 行业动态 更新时间:2024-10-26 15:28:10
我怎样才能实时向后工作者报告progresschanged事件的目录数量?(How can i report to the backgroundworker progresschanged event in real time the number of directories?)

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

In this two methods GetDirectories and MyGetDirectories i'm getting recursive all the directories and sub directories. I want to display on label2 in the progresschanged event like a counter that count the number of directories until it finish.

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

Then the GetDirectories method

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; }

And the method 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>(); } }

What I tried is in the method MyGetDirectories I added a counter called the variable countDirectories and I checked it does count.

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>(); } }

Then in the DoWork event I did:

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

And in the progresschanged event

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

But it's not reporting to the label2 anything I guess since it's still working inside the GetDirectories method. So i'm stuck here.

最满意答案

这是只有按钮和标签的表单代码:

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>(); } } } }

This is code of form with just button and label on it:

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>(); } } } }

更多推荐

本文发布于:2023-07-24 00:18:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1239172.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:工作者   实时   数量   事件   目录

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!