Search code examples
c#.netwinformsbackgroundworker

Background worker "cause" to freeze my form


In my app I want create thousands files in background, but when I execute this it always freeze my form(after all files were created I can work with my form again). How run it properly without hanging?

private void button5_Click(object sender, EventArgs e)
    {
        BackgroundWorker bw = new BackgroundWorker();

        bw.WorkerReportsProgress = true;

        bw.DoWork += new DoWorkEventHandler(
        delegate(object o, DoWorkEventArgs args)
        {
            BackgroundWorker b = o as BackgroundWorker;

            this.Invoke(new MethodInvoker(delegate
            {
                getValues();//load some text fields into strings

                while (counter < counted)
                {
                    text = richTextBox1.Text;
                    text = text.Replace("number", finalNumber);

                    //create copies
                    if (checkBox1.Checked == true)
                    {
                        while (createdCopies < copies)
                        {
                            createdCopies++;

                            File.WriteAllText(fileName, text);
                            overalCounter++;
                            b.ReportProgress(overalCounter);
                        }
                        counter++;
                        createdCopies = 0;
                    }
                    //dont create copies
                    else
                    {
                        File.WriteAllText(fileName, text);
                        counter++;
                        overalCounter++;
                        b.ReportProgress(overalCounter);
                    }
                    //info about number of files created
                    label6.Text = "created " + overalCounter.ToString() + " files";

                }
                label1.Text = "success";
            }));
    });

    if (bw.IsBusy != true)
    {
        bw.RunWorkerAsync();
    }

    bw.ProgressChanged += new ProgressChangedEventHandler(
    delegate(object o, ProgressChangedEventArgs args)
    {
        this.Text = string.Format("{0}% Completed", args.ProgressPercentage);
    });
    }

Solution

  • this.Invoke runs the code inside on the UI thread, blocking any UI updates.
    Since you run everything in the Invoke method everything will run on the UI thread.

    Create a separate Invoke around each place where you modify your UI controls and leave the heavy work outside the Invokes.