1、Invoke() 调用时,Invoke会阻止当前主线程的运行,等到 Invoke() 方法返回才继续执行后面的代码,表现出“同步”的概念。

2、BeginInvoke() 调用时,当前线程会启用线程池中的某个线程来执行此方法,BeginInvoke不会阻止当前主线程的运行,而是等当前主线程做完事情之后再执行BeginInvoke中的代码内容,表现出“异步”的概念。在想获取 BeginInvoke() 执行完毕后的结果时,调用EndInvoke() 方法来获取。而这两个方法中执行的是一个委托。

测试代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks; namespace InvokeDemo
{
class Program
{
//代理
private delegate int Mydelegate(int a, int b); static void Main(string[] args)
{
Console.WriteLine("Invoke开始,时间:" + DateTime.Now);
Console.WriteLine("主线程ID:" + Thread.CurrentThread.ManagedThreadId);
Mydelegate myd = new Mydelegate(Fun);
var result = myd.Invoke(, );
Console.WriteLine("计算结果:" + result);
Console.WriteLine("Invoke结束,时间:" + DateTime.Now); Console.WriteLine();
Console.WriteLine("------分割线------");
Console.WriteLine(); Console.WriteLine("BeginInvoke开始,时间:" + DateTime.Now);
Mydelegate mydSync = new Mydelegate(Fun);
mydSync.BeginInvoke(, , new AsyncCallback(AsyncCallback), mydSync);
Console.WriteLine("BeginInvoke结束,时间:" + DateTime.Now); Console.Read();
} static int Fun(int a, int b)
{
Thread.Sleep();
Console.WriteLine("方法线程ID:" + Thread.CurrentThread.ManagedThreadId);
return a + b;
} static void AsyncCallback(IAsyncResult asyncResult)
{
var del = (Mydelegate)asyncResult.AsyncState;
var funResult = del.EndInvoke(asyncResult);
Console.WriteLine("计算结果:" + funResult);
}
}
}

结果:

可以看出:

Invoke方法中线程ID跟主线程ID一致,表现是阻塞的。

InvokeBegin方法中线程ID跟主线程ID不一致,表现是异步的。

C#中Invoke和BeginInvoke的区别的更多相关文章

  1. C#中Invoke 和 BeginInvoke 的区别

    Control.Invoke 方法 (Delegate) :在拥有此控件的基础窗口句柄的线程上执行指定的委托. Control.BeginInvoke 方法 (Delegate) :在创建控件的基础句 ...

  2. .Net基础——程序集与CIL HttpClient封装方法 .Net Core 编码规范 C#中invoke和beginInvoke的使用 WebServeice 动态代理类

    .Net基础——程序集与CIL   1. 程序集和CIL: 程序集是由.NET语言的编译器接受源代码文件产生的输出文件,通常分为 exe和dll两类,其中exe包含Main入口方法可以双击执行,dll ...

  3. Invoke 和 BeginInvoke 的区别(转发)

    在Invoke或者BeginInvoke的使用中无一例外地使用了委托Delegate. 一.为什么Control类提供了Invoke和BeginInvoke机制? 关于这个问题的最主要的原因已经是do ...

  4. Invoke 与 BeginInvoke的区别

    引用文章路径:https://www.cnblogs.com/lsgsanxiao/p/5523282.html invoke和begininvoke 区别 一直对invoke和begininvoke ...

  5. Dispatcher中Invoke与BeginInvoke

    [同步]Invoke Application.Current.Dispatcher.Invoke(AutoIncreaseNumber); [异步]BeginInvoke Application.Cu ...

  6. Invoke 和 BeginInvoke 的区别

    在Invoke或者BeginInvoke的使用中无一例外地使用了委托Delegate 一.为什么Control类提供了Invoke和BeginInvoke机制? 关于这个问题的最主要的原因已经是dot ...

  7. Invoke和BeginInvoke的区别(转载)

    转自http://www.cnblogs.com/c2303191/articles/826571.html Control.Invoke 方法 (Delegate) :在拥有此控件的基础窗口句柄的线 ...

  8. WPF Invoke与BeginInvoke的区别

    Control.Invoke 方法 (Delegate) :在拥有此控件的基础窗口句柄的线程上执行指定的委托. Control.BeginInvoke 方法 (Delegate) :在创建控件的基础句 ...

  9. Unity中Invoke 和 InvokeRepeating的区别

    Invoke() 方法是 Unity3D 的一种委托机制 如: Invoke("Test", 5);   它的意思是:5 秒之后调用 Test() 方法: 使用 Invoke() ...

随机推荐

  1. css样式被覆盖解决方案

    刚才写zenktodo的时候,通过动态添加class的方式修改一个div的样式,总是不起作用. #navigator { height: 100%; width: 200; position: abs ...

  2. MVC的System.Web.Mvc.ViewPage小结

    Inherits="System.Web.Mvc.ViewPage<dynamic>这一句最好是自己手动修改,如果是维护用户数据,用户对象名是User,改成Inherits=&q ...

  3. redis的发布订阅模式

    概要 redis的每个server实例都维护着一个保存服务器状态的redisServer结构 struct redisServer {     /* Pubsub */     // 字典,键为频道, ...

  4. DBCP连接池介绍

    DBCP连接池介绍 ----------------------------- 目前 DBCP 有两个版本分别是 1.3 和 1.4. DBCP 1.3 版本需要运行于 JDK 1.4-1.5 ,支持 ...

  5. discuz问题综合

    1.discuz x2搬家问题:搬家后,需要修改数据库的配置文件,包含:      主要的配置文件有三个:config目录下的config_global.php和config_ucenter.php以 ...

  6. ajax跳转页面问题

    $.ajax({ type:"POST", url: //你的请求程序页面随便啦 async:false,//同步:意思是当有返回值以后才会进行后面的js程序. data://请求 ...

  7. 关于C#中派生类调用基类构造函数的理解

    (1)当基类中没有自己编写的构造函数时,派生类默认条用基类的构造函数 (2)当基类中有自己编写的构造函数时,要在基类中添加无参的构造函数 public class MyBaseClass { publ ...

  8. linux C 9*9

    #include<stdio.h> #include<conio.h> #include <windows.h> void Gotoxy(int x, int y) ...

  9. C# OpenFileDialog和PictrueBox

     string resultFile = ""; OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFile ...

  10. java课堂练习之可变參数与卫条件

    /*  有人邀请A,B,C,D,E,F 6个人參加一项会议,这6个人有些奇怪.由于他们有非常多要求,已知:  1)A,B两人至少有1人參加会议:  2)A,E,F 3人中有2人參加会议.  3)B和C ...