TheBoubou
TheBoubou

Reputation: 19933

Winforms : avoid freeze application

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 :

  1. a solution to not freeze the form
  2. 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();
}

Upvotes: 3

Views: 7304

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039408

The application freezes because you are performing the file parsing in the main thread. You could use BackgroundWorker to perform operation on a new thread. Here's some pseudo code that might help you to get started:

private void button1_Click(object sender, EventArgs e)
{
    var worker = new BackgroundWorker();
    worker.DoWork += (s, args) =>
    {
        // Here you perform the operation and report progress:
        int percentProgress = ...
        string currentFilename = ...
        ((BackgroundWorker)s).ReportProgress(percentProgress, currentFilename);
        // Remark: Don't modify the GUI here because this runs on a different thread
    };
    worker.ProgressChanged += (s, args) =>
    {
        var currentFilename = (string)args.UserState;
        // TODO: show the current filename somewhere on the UI and update progress
        progressBar1.Value = args.ProgressPercentage;
    };
    worker.RunWorkerCompleted += (s, args) =>
    {
        // Remark: This runs when the DoWork method completes or throws an exception
        // If args.Error != null report to user that something went wrong
        progressBar1.Value = 0;
    };
    worker.RunWorkerAsync();
}

Upvotes: 20

Related Questions