The true meaning of Invoke and BeginInvoke in C

  • 2020-11-20 06:13:00
  • OfStack

Is the BeginInvoke method really an asynchronous call made by a new thread?

Refer to the following code:


public delegate void treeinvoke();
private void UpdateTreeView()
{
  MessageBox.Show(System.Threading.Thread.CurrentThread.Name);
}
private void button1_Click(object sender, System.EventArgs e)
{
  System.Threading.Thread.CurrentThread.Name = "UIThread";
  treeView1.BeginInvoke(new treeinvoke(UpdateTreeView));
}

Looking at the results of the run, the pop-up dialog shows UIThread, indicating that the delegate invoked by BeginInvoke is actually executed in the UI thread.

Why "asynchronous execution" 1 when you are executing in an UI thread?

Let's look at the following code:


public delegate void treeinvoke();
private void UpdateTreeView()
{
  MessageBox.Show(Thread.CurrentThread.Name);
}
private void button1_Click(object sender, System.EventArgs e)
{
  Thread.CurrentThread.Name = "UIThread";
  Thread th = new Thread(new ThreadStart(StartThread));
  th.Start();
}
private void StartThread()
{
  Thread.CurrentThread.Name = "Work Thread";
  treeView1.BeginInvoke(new treeinvoke(UpdateTreeView));
}

If you look at the results of the run, the dialog box that pops up again shows UIThread. What does that mean? This indicates that the delegate invoked by the BeginInvoke method is executed in the UI thread anyway.

So what is BeginInvoke really good for?

In a multithreaded programming, we often have to in a worker thread to update the display interface, and in a multithreaded direct call interface control method is wrong, the specific reason can be finished after this I see this article: how to call in multithreaded Winform, if you are and don't look at me this article, directly see the article, anyway, I haven't read that article.

Invoke and BeginInvoke are designed to solve this problem, allowing you to safely update your interface display in multiple threads.

The correct approach is to encapsulate the code in the worker thread that involves updating the interface into one method that is invoked via Invoke or BeginInvoke, the difference being that one causes the worker thread to wait while the other does not.

The so-called "response operation, 1 side add node" forever is only relative, not too burdened UI thread in, because the correct interface updates always through UI thread to do it, we want to do is do most of the operations in the worker thread, and will update on UI thread interface of the pure, so also to reach the purpose to reduce the financial burden of UI thread.

And during that update the tree node code, actually only play a role of the code is: 1. System Threading. Thread. Sleep (100); , it gives the UI thread a chance to process interface messages, but the digital ghost complicates the problem and the following code works fine.


private void button1_Click_(object sender, System.EventArgs e)
{
  TreeNode tn;
  for(int i=0;i<100000;i++)
  {
    tn=new TreeNode (i.ToString());
    this.treeView1.Nodes[0].Nodes.Add(tn);
    if (i%100 == 0) Application.DoEvents();
  }
}

Related articles: