Search code examples
c#backgroundworkerform-load

Form1_Load is not called (attempt to have a BackgroundWorker)


I am developing simple Windows Forms Application in C#

I want to show a GUI and then start some tasks in the background. I would like to show progress of these tasks using text label in the forms.

Program.cs is pretty straightforward:

   static void Main()
    {      
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        Form mainForm = (new Form1());

        Application.Run(mainForm);
    }

For the label i use binding

partial class Form1 : INotifyPropertyChanged
{
    public string Title
    {
        get { return _title; }
        set
        {
            _title = value;
            if(PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("Title"));
            }
        }


private void InitializeComponent()
    {
        this.label1 = new System.Windows.Forms.Label();
        this.SuspendLayout();
        // 
        // label1
        // 
        this.label1.AutoSize = true;
        this.label1.DataBindings.Add(new System.Windows.Forms.Binding("Text", this, "Title", true));
        this.label1.Location = new System.Drawing.Point(12, 9);
        this.label1.Name = "label1";
        this.label1.Size = new System.Drawing.Size(46, 17);
        this.label1.TabIndex = 0;
        this.label1.Text = "label1";
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(763, 255);
        this.Controls.Add(this.label1);
        this.Name = "Form1";
        this.Text = "Form1";
        this.ResumeLayout(false);
        this.PerformLayout();


    }
 private string _title = "xxgg";
    private System.Windows.Forms.Label label1;

    public event PropertyChangedEventHandler PropertyChanged;

And then I would like to run a background worker, but for some reason the Form1_Load method is not executed.

 private void Form1_Load(object sender, System.EventArgs e)
    {
        System.Console.WriteLine("Form loaded");

        var bw = new BackgroundWorker();
        bw.DoWork += bw_DoWork;
        bw.RunWorkerAsync(); //Start the worker
    }

void bw_DoWork(object sender, DoWorkEventArgs e)
    {
        InfoText it = new InfoText();
        FileMover fm = new FileMover(it);

        foreach(string path in sourcePaths)
        {
            fm.MoveFrom(path);
            this.Title = it.text;
        }
    }

As you can see I want to update the Title while running the background worker.

What is the reason behind my Form1_Load not being called? Thank you

Edit: I have added the BackgroundWorker code in the Form1 class like this:

 public partial class Form1 : Form
{
    BackgroundWorker bw;

    public Form1()
    {
        InitializeComponent();           

        it = new InfoText();

        bw = new BackgroundWorker();
        bw.DoWork += bw_DoWork;
        bw.WorkerReportsProgress = true;
        bw.ProgressChanged += Bw_ProgressChanged;
        bw.RunWorkerCompleted += Bw_RunWorkerCompleted;
        bw.RunWorkerAsync(); //Start the worker
    }

    private void Bw_RunWorkerCompleted(object sender,   RunWorkerCompletedEventArgs e)
    {
        Console.WriteLine("done");

        this.Title = it.text;
    }

    private void Bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        Console.WriteLine("progress changed " + e.ProgressPercentage);

        this.Title = it.text;
    }

    void bw_DoWork(object sender, DoWorkEventArgs e)
    {
        FileMover fm = new FileMover(it);

        foreach(string path in sourcePaths)
        {
            fm.MoveFrom(path);
            bw.ReportProgress(1);
        }
    }
}

Now it does exactly what I wanted.


Solution

  • Add

    this.Load += new System.EventHandler(Form1_Load);
    

    Inside InitializeComponent... or class constructor.

    I would like to add it via the GUI designer, it's safer and easier.