the problem is that when it's doing the foreach loop the whole application freeze until the foreach loop end.
As you said, you can use a BackgroundWorker or a Thread
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
dic = new Dictionary<string, long>();
var filesl = GetFiles(@"D:\", "*.*").ToList();
foreach (var item in filesl)
{
FileInfo info = new FileInfo(item);
if (File.Exists(info.FullName))
{
dic.Add(item, info.Length);
}
}
I'm using wpf and c#
the foreach loop takes a lot of time. i thought to use backgroundworker or something else that will make the foreach async maybe.
i want to give the user some notice on label maybe that it's getting files like "getting files progress". the problem is that when it's doing the foreach loop the whole application freeze until the foreach loop end.
the problem is that when it's doing the foreach loop the whole application freeze until the foreach loop end.
As you said, you can use a BackgroundWorker or a Thread
I would simply show progress by file name in a label using code as per below (written in .NET Core). The only thing I did not include was code to cancel which can be done with a CancellationToken
public class FileOperations
{
public delegate void OnTraverse(string sender);
public event OnTraverse Traverse;
public delegate void OnDone();
public event OnDone Done;
public async Task CollectFiles(string path, string searchPattern, SearchOption searchOption = SearchOption.TopDirectoryOnly)
{
using var enumerator = await Task.Run(() => Directory.EnumerateFiles(path, searchPattern, searchOption).GetEnumerator());
while (await Task.Run(() => enumerator.MoveNext()))
{
Traverse?.Invoke(enumerator.Current);
}
Done?.Invoke();
}
}
Form code
using System;
using System.IO;
using System.Windows.Forms;
using IterateFoldersApp.Classes;
namespace IterateFoldersApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private async void button1_Click(object sender, EventArgs e)
{
FileOperations operations = new();
operations.Traverse += OperationsOnTraverse;
operations.Done += OnDone;
await operations.CollectFiles("TODO", "*.*", SearchOption.AllDirectories);
}
private void OnDone()
{
MessageBox.Show("Done");
}
private void OperationsOnTraverse(string sender)
{
FileInfo info = new (sender);
if (File.Exists(sender))
{
label1.Text = $"Working {info.Name}";
}
}
}
}
Edit: If you want to figure out total files
private static int FileCount(string directory)
{
DirectoryInfo dirInfo = new(directory);
return dirInfo.EnumerateDirectories()
.AsParallel()
.SelectMany(di => di.EnumerateFiles("*.*", SearchOption.AllDirectories))
.Count();
}
Then look at IProgress<int>
and Report off IProgress.