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

推荐订阅源

Know Your Adversary
Know Your Adversary
云风的 BLOG
云风的 BLOG
Recent Announcements
Recent Announcements
F
Fortinet All Blogs
B
Blog
罗磊的独立博客
宝玉的分享
宝玉的分享
Vercel News
Vercel News
Martin Fowler
Martin Fowler
N
Netflix TechBlog - Medium
P
Proofpoint News Feed
T
Threatpost
Security Latest
Security Latest
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Stack Overflow Blog
Stack Overflow Blog
I
Intezer
P
Privacy International News Feed
D
Docker
月光博客
月光博客
博客园 - 三生石上(FineUI控件)
M
MIT News - Artificial intelligence
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
L
Lohrmann on Cybersecurity
Google DeepMind News
Google DeepMind News
The Last Watchdog
The Last Watchdog
A
Arctic Wolf
IT之家
IT之家
S
SegmentFault 最新的问题
S
Securelist
博客园 - 叶小钗
N
News and Events Feed by Topic
F
Full Disclosure
Security Archives - TechRepublic
Security Archives - TechRepublic
Engineering at Meta
Engineering at Meta
Hacker News: Ask HN
Hacker News: Ask HN
博客园 - Franky
GbyAI
GbyAI
AI
AI
Y
Y Combinator Blog
WordPress大学
WordPress大学
Latest news
Latest news
Microsoft Security Blog
Microsoft Security Blog
人人都是产品经理
人人都是产品经理
N
News | PayPal Newsroom
The Cloudflare Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
I
InfoQ

博客园 - 王跃军

成本安全硬件(二):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);
        }
    }