惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Engineering at Meta
Engineering at Meta
博客园_首页
H
Help Net Security
WordPress大学
WordPress大学
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
罗磊的独立博客
博客园 - 三生石上(FineUI控件)
B
Blog
I
InfoQ
SecWiki News
SecWiki News
T
Tailwind CSS Blog
Spread Privacy
Spread Privacy
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
V
Vulnerabilities – Threatpost
N
Netflix TechBlog - Medium
P
Palo Alto Networks Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Vercel News
Vercel News
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
K
Kaspersky official blog
M
MIT News - Artificial intelligence
S
Schneier on Security
T
Threat Research - Cisco Blogs
F
Fortinet All Blogs
Cyberwarzone
Cyberwarzone
Scott Helme
Scott Helme
aimingoo的专栏
aimingoo的专栏
Martin Fowler
Martin Fowler
MyScale Blog
MyScale Blog
The Cloudflare Blog
Recent Announcements
Recent Announcements
Security Latest
Security Latest
G
GRAHAM CLULEY
IT之家
IT之家
Y
Y Combinator Blog
The Last Watchdog
The Last Watchdog
腾讯CDC
Google DeepMind News
Google DeepMind News
V
V2EX
S
Securelist
TaoSecurity Blog
TaoSecurity Blog
B
Blog RSS Feed
S
SegmentFault 最新的问题
博客园 - 叶小钗
P
Proofpoint News Feed
云风的 BLOG
云风的 BLOG
Project Zero
Project Zero
G
Google Developers Blog
Google DeepMind News
Google DeepMind News
F
Full Disclosure

博客园 - 王跃军

成本安全硬件(二):RFID on PN532 之WINDOWS 环境应用 vb中使用Xtreme Command Bars ActiveX Control 的IPrintView接口绘制图形_后记 vb中使用Xtreme Command Bars ActiveX Control 的IPrintView接口绘制图形 android开发专题系列-OPENGL android开发专题系列-Android开发指南 android开发专题系列-一个简单的游戏的设计 android网站汇集 android开发资料(DEVELOPER.ANDROID.COM/SDK)WORD版 可也extJs相媲美的js 控件集 DHTMLX 对ASP.NET 安全方面的一些理解 ANDROID 开发资源网站列表(2008年1月14日) [转]林西:企业信息化规划方法 [收藏]DIV+CSS布局积累 [转]常用SQL 深入实例理解 [转]可用表格样式表 DataSet 加载XML 数据效果 纯ajax式,多用户选择示例 asp.net全局异常处理 也nhibernate会遇到问题,还好找到原因了
C# 委托的三种调用示例(同步调用 异步调用 异步回调) 之 FUNC 版
王跃军 · 2017-03-06 · via 博客园 - 王跃军

园子里,一篇精典 介绍 C#委托方法是基于传统定义委托,并实现 调用。但C# 5.0 提供了泛型委托,能更高效的调用,本人引用原代码,进行改写

供大家参考

 public class 加法类
    {
        public static int Add(int a, int b)
        {
            Console.WriteLine("开始计算:" + a + "+" + b);
            Thread.Sleep(3000); //模拟该方法运行三秒
            Console.WriteLine("计算完成!");
            return a + b;
        }
    }

    public class 同步调用
    {
        static void Main()
        {
            Console.WriteLine("===== 同步调用 SyncInvokeTest =====");
            Func<int, int,int> handler = 加法类.Add;
            int result = handler(1, 2);

            Console.WriteLine("继续做别的事情。。。");
            Console.WriteLine(result);
            Console.ReadKey();
        }
    }
    public class 异步调用
    {
        static void Main()
        {
            Console.WriteLine("===== 异步调用 AsyncInvokeTest =====");
            Func<int, int,int> handler = 加法类.Add;
            //IAsyncResult: 异步操作接口(interface)
            //BeginInvoke: 委托(delegate)的一个异步方法的开始
            IAsyncResult result = handler.BeginInvoke(1, 2, null, null);
            Console.WriteLine("继续做别的事情。。。");
            //异步操作返回
            Console.WriteLine(handler.EndInvoke(result));
            Console.ReadKey();
        }
    }
    public class 异步回调
    {
        static void Main()
        {
            Console.WriteLine("===== 异步回调 AsyncInvokeTest =====");
            Func<int, int, int> handler = 加法类.Add;
            //异步操作接口(注意BeginInvoke方法的不同!)
            IAsyncResult result = handler.BeginInvoke(1, 2, new AsyncCallback(回调函数), "AsycState:OK");
            Console.WriteLine("继续做别的事情。。。");
            Console.ReadKey();
        }
        static void 回调函数(IAsyncResult result)
        {
            //result 是“加法类.Add()方法”的返回值
            //AsyncResult 是IAsyncResult接口的一个实现类,引用空间:System.Runtime.Remoting.Messaging
            //AsyncDelegate 属性可以强制转换为用户定义的委托的实际类。
            //AddHandler handler = (AddHandler)((AsyncResult)result).AsyncDelegate;
            Func<int, int, int> handler = (Func<int, int, int>)((AsyncResult)result).AsyncDelegate; ;
            Console.WriteLine(handler.EndInvoke(result));
            Console.WriteLine(result.AsyncState);
        }
    }