Diffrernce between BackgroundWorker.ReportProgress() and Control.Invoke()

Posted by ohadsc on Stack Overflow See other posts from Stack Overflow or by ohadsc
Published on 2010-05-19T13:39:03Z Indexed on 2010/05/19 13:50 UTC
Read the original article Hit count: 241

What is the difference between options 1 and 2 in the following?

    private void BGW_DoWork(object sender, DoWorkEventArgs e)
    {
        for (int i=1; i<=100; i++)
        {
            string txt = i.ToString();
            if (Test_Check.Checked)
                //OPTION 1
                Test_BackgroundWorker.ReportProgress(i, txt); 
            else
                //OPTION 2
                this.Invoke((Action<int, string>)UpdateGUI, new object[] {i, txt});
        }
    }

    private void BGW_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        UpdateGUI(e.ProgressPercentage, (string)e.UserState);
    }

    private void UpdateGUI(int percent, string txt)
    {
        Test_ProgressBar.Value = percent;
        Test_RichTextBox.AppendText(txt + Environment.NewLine);
    }

Looking at reflector, the Control.Invoke() appears to use:

this.FindMarshalingControl().MarshaledInvoke(this, method, args, 1);

whereas BackgroundWorker.Invoke() appears to use:

this.asyncOperation.Post(this.progressReporter, args);

(I'm just guessing these are the relevant function calls.) If I understand correctly, BGW Posts to the WinForms window its progress report request, whereas Control.Invoke uses a CLR mechanism to invoke on the right thread. Am I close? And if so, what are the repercussions of using either ?

Thanks

© Stack Overflow or respective owner

Related posts about backgroundworker

Related posts about winforms