简单的讲就是

  1. BeginInvoke不需要等待方法运行完毕,就会继续执行下面的代码
  2. Invoke则必须等待Invoke中的代码运行完毕,才会继续执行下面的代码。

可以通过下面的项目测试上面所描述的观点。

在代码中的一段代码如下:

 1         /// <summary>
 2         /// Sub thread function
 3         /// </summary>
 4         private void ThreadFun()
 5         {
 6             MethodInvoker mi = new MethodInvoker(ShowProcessBar);
 7             
 8             this.BeginInvoke(mi);
 9             //this.Invoke(mi);
10 
11             MessageBox.Show("可以根据弹出的警告框先后次序判断BeginInvoke与Invoke的区别");
12 
13             Thread.Sleep(1000);//Sleep a while to show window
14 
15             bool blnIncreased = false;
16             object objReturn = null;
17             do
18             {
19                 Thread.Sleep(50);
20                 objReturn = this.Invoke(this.myIncrease,
21                     new object[] { 1 });
22                 blnIncreased = (bool)objReturn;
23             }
24             while (blnIncreased);
25         }

通过切换注释

BeginInvoke与Invoke的效果来体会两者的区别

 

项目下载地址:【点这里】

 

相关文章:

  • 2022-12-23
  • 2021-12-04
  • 2021-08-19
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-09-23
  • 2021-12-29
  • 2021-07-07
  • 2021-12-07
相关资源
相似解决方案