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)
Test_BackgroundWorker.ReportProgress(i, txt);
else
this.BeginInvoke((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);
}
When looking at the reflector, Control.BeginInvoke () will use:
this.FindMarshalingControl().MarshaledInvoke(this, method, args, 1);
Which seems to end up calling some native functions like PostMessage (), couldn’t pinpoint the stream from the reflector (optimizing the compiler optimizer)
While BackgroundWorker.Invoke () seems to use:
this.asyncOperation.Post(this.progressReporter, args);
Which seems to end up calling ThreadPool.QueueUserWorkItem ()
( , .) , ThreadPool , Post . , ? ( - - , , , , .)
!