C#:Func的同步、异步调用(转)
实际开发中,对于一些耗时较长的操作,我们往往会将其封装成异步方式调用,以加速系统响应或改善用户体验,下面是一个示例:
有一个现成的类MyMath,里面有一个Add方法:

1 public class MyMath
2 {
3
4 public int Add(int a, int b)
5 {
6 System.Threading.Thread.Sleep(5000);
7 return a + b;
8 }
9
10 }

对Add方法做下封装,对了对比,同时提供“同步调用”与"异步调用"二个版本:

1 /// 异步调用
2 /// </summary>
3 /// <param name="a"></param>
4 /// <param name="b"></param>
5 /// <param name="callBackAction"></param>
6 /// <returns></returns>
7 static void AyscAdd(int a, int b, Action<int> callBackAction)
8 {
9 Func<int> func = () =>
10 {
11 return new MyMath().Add(a, b);
12 };
13 func.BeginInvoke((ar) =>
14 {
15 var result = func.EndInvoke(ar);
16 callBackAction.Invoke(result);
17 },
18 null);
19
20 }
21
22 /// <summary>
23 /// 同步调用
24 /// </summary>
25 /// <param name="a"></param>
26 /// <param name="b"></param>
27 /// <returns></returns>
28 static int SyncAdd(int a, int b)
29 {
30 return new MyMath().Add(a, b);
31 }

最后调用验证:

1 static void Main(string[] args)
2 {
3 Console.WriteLine("同步调用开始=>");
4 int a = SyncAdd(1, 2);
5 Console.WriteLine("同步调用结束:" + a);
6
7 Console.WriteLine("--------------------------");
8
9 Console.WriteLine("异步调用开始=>");
10 AyscAdd(3, 4, (result) =>
11 {
12 Console.WriteLine("异步调用结果:" + result);
13 });
14 Console.WriteLine("异步调用结束");
15
16 Console.ReadLine();
17 }

完整代码:

1 using System;
2
3 namespace ActionDemo
4 {
5 class Program
6 {
7 static void Main(string[] args)
8 {
9 Console.WriteLine("同步调用开始=>");
10 int a = SyncAdd(1, 2);
11 Console.WriteLine("同步调用结束:" + a);
12
13 Console.WriteLine("--------------------------");
14
15 Console.WriteLine("异步调用开始=>");
16 AyscAdd(3, 4, (result) =>
17 {
18 Console.WriteLine("异步调用结果:" + result);
19 });
20 Console.WriteLine("异步调用结束");
21
22 Console.ReadLine();
23 }
24
25 /// <summary>
26 /// 异步调用
27 /// </summary>
28 /// <param name="a"></param>
29 /// <param name="b"></param>
30 /// <param name="callBackAction"></param>
31 /// <returns></returns>
32 static void AyscAdd(int a, int b, Action<int> callBackAction)
33 {
34 Func<int> func = () =>
35 {
36 return new MyMath().Add(a, b);
37 };
38 func.BeginInvoke((ar) =>
39 {
40 var result = func.EndInvoke(ar);
41 callBackAction.Invoke(result);
42 },
43 null);
44
45 }
46
47 /// <summary>
48 /// 同步调用
49 /// </summary>
50 /// <param name="a"></param>
51 /// <param name="b"></param>
52 /// <returns></returns>
53 static int SyncAdd(int a, int b)
54 {
55 return new MyMath().Add(a, b);
56 }
57 }
58
59 public class MyMath
60 {
61
62 public int Add(int a, int b)
63 {
64 System.Threading.Thread.Sleep(5000);
65 return a + b;
66 }
67
68 }
69 }

输出结果如下:
同步调用开始=>
同步调用结束:3
--------------------------
异步调用开始=>
异步调用结束
异步调用结果:7
C#:Func的同步、异步调用(转)的更多相关文章
- dubbo同步/异步调用的方式
我们知道,Dubbo 缺省协议采用单一长连接,底层实现是 Netty 的 NIO 异步通讯机制:基于这种机制,Dubbo 实现了以下几种调用方式: 同步调用(默认) 异步调用 参数回调 事件通知 同步 ...
- .Net下的MSMQ(微软消息队列)的同步异步调用
一.MSMQ简介 MSMQ(微软消息队列)是Windows操作系统中消息应用程序的基础,是用于创建分布式.松散连接的消息通讯应用程序的开发工具.消息队列 和电子邮件有着很多相似处,他们都包含多个属性, ...
- jQuery同步/异步调用后台方法
$.ajax({ type: "Post", url: "UserManage.aspx/SubmitPage",//页面/方法名 data: "{' ...
- ajax 同步异步调用
- C#:Func的同步、异步调用
using System; namespace ActionDemo { class Program { static void Main(string[] args) { Console.Write ...
- C# Func的同步、异步调用
using System; namespace ActionDemo { class Program { static void Main(string[] args) { Console.Write ...
- C#的同步和异步调用方法
同步和异步大家都明白什么意思,在这里不多介绍了. namespace ConsoleTest { class Program { static void Main(string[] args) { C ...
- python 并发编程 同步调用和异步调用 回调函数
提交任务的两张方式: 1.同步调用 2.异步调用 同步调用:提交完任务后,就在原地等待任务执行完后,拿到结果,再执行下一行代码 同步调用,导致程序串行执行 from concurrent.future ...
- Python并发编程06 /阻塞、异步调用/同步调用、异步回调函数、线程queue、事件event、协程
Python并发编程06 /阻塞.异步调用/同步调用.异步回调函数.线程queue.事件event.协程 目录 Python并发编程06 /阻塞.异步调用/同步调用.异步回调函数.线程queue.事件 ...
随机推荐
- every day english
job is in your freedom, not your compliance. through no fault of his own. as far as I understand you ...
- 【LeetCode 239】Sliding Window Maximum
Given an array nums, there is a sliding window of size k which is moving from the very left of the a ...
- 三角剖分求多边形面积的交 HDU3060
//三角剖分求多边形面积的交 HDU3060 #include <iostream> #include <cstdio> #include <cstring> #i ...
- sql server 2008 r2 出问题
1.想利用sql2008的数据挖掘功能,以为是没有安装全,所以就卸载了. (1)利用Windows Installer Clean UP将以前的卸载干净 (2)出现了Could not open ke ...
- QS之warning message
Multiple message categories are specified as a comma separated list.
- leetcode—jump game
1.题目描述 Given an array of non-negative integers, you are initially positioned at the first index of t ...
- 【WPF】ContentControl Style定义与使用出现问题后 -- 引发的思考
一.背景 使用WPF的朋友,大家都很喜欢采用定义控件的公共样式,以便整个框架对该资源的使用,好处就是可以达到代码复用.系统风格统一等: 1. 定义资源 <Style TargetT ...
- QCon2013上海站总结 -- 整体印象和感悟
基本情况: QCon 2013(http://www.qconshanghai.com/)上海站的活动一共为期3天(周五.六和日).活动在上海的光大会展中心举行的. QCon(全球软件开发者大会)是由 ...
- 怎么从sqlserver 数据库导出 insert 的数据语句
In SSMS in the Object Explorer, right click on the database right-click and pick "Tasks" a ...
- Python程序的混淆和加密
混淆 为了增加代码阅读的难度, 源代码的混淆非常必要, 一个在线的Python代码混淆网站. 如果你觉得有用, 可以购买离线版本.同时需要注意的是, 这个混淆其实还是被很多人怀疑的, 因为即使混淆了, ...