Hello,
I do some operation on "big" file (around 4Mb)
I do this : 1. Get all files from a directory and place them in an IList MyInfoClass has properties : name, extension, fullPath, creationDate, contentPart 2. I do a Linq query to get only some extension type. 3. I loop on the Linq query result and for each, I open the file, do some operation (get value) and put the result in MyFileIno.ContentPart.
FYI : 30 files, it's a 14sec operation
That's work.
The problem, when I run my library from the UI, when I click the button, the window freeze during operation time. I'd like :
- a solution to not freeze the form
- see the progress operation
Could you give me the best practice to solve this kind of problem ?
Thanks,
Code
public class FileManager
{
public string CurrentFileName { get; set; }
public void Validation(string path)
{
IList<InfoFile> listFile = GetListFile(path);
foreach (InfoFile item in listFile)
{
CurrentFileName = item.Name;
.....
}
}
}
private void button1_Click(object sender, EventArgs e)
{
var worker = new BackgroundWorker();
worker.DoWork += (s, args) =>
{
int percentProgress = 6;
FileManager fileManager = new FileManager();
fileManager.Validation(@"C:.....");
((BackgroundWorker)s).ReportProgress(percentProgress, fileManager.CurrentFileName);
};
worker.ProgressChanged += (s, args) =>
{
var currentFilename = (string)args.UserState;
label1.Text = currentFilename;
progressBar1.Value = args.ProgressPercentage;
};
worker.RunWorkerCompleted += (s, args) =>
{
progressBar1.Value = 0;
};
worker.RunWorkerAsync();
}