解析C#中委托的同步调用与异步调用(实例详解)
时间:2022-12-24 19:06:47
委托的Invoke方法用来进行同步调用。同步调用也可以叫阻塞调用,它将阻塞当前线程,然后执行调用,调用完毕后再继续向下进行。
同步调用的例子:
using System;
using System.Threading;
public delegate int AddHandler(int a, int b);
public class Foo {
static void Main() {
Console.WriteLine("**********SyncInvokeTest**************");
AddHandler handler = new AddHandler(Add);
int result = handler.Invoke(1,2);
Console.WriteLine("Do other work... ... ...");
Console.WriteLine(result);
Console.ReadLine();
}
static int Add(int a, int b) {
Console.WriteLine("Computing "+a+" + "+b+" ...");
Thread.Sleep(3000);
Console.WriteLine("Computing Complete.");
return a+b;
}
}运行结果:
**********SyncInvokeTest**************
Computing 1 + 2 ...
Computing Complete.
Do other work... ... ...
同步调用会阻塞线程,如果是要调用一项繁重的工作(如大量IO操作),可能会让程序停顿很长时间,造成糟糕
的用户体验,这时候异步调用就很有必要了。
异步调用不阻塞线程,而是把调用塞到线程池中,程序主线程或UI线程可以继续执行。
委托的异步调用通过BeginInvoke和EndInvoke来实现。
异步调用:
using System;
using System.Threading;
public delegate int AddHandler(int a, int b);
public class Foo {
static void Main() {
Console.WriteLine("**********AsyncInvokeTest**************");
AddHandler handler = new AddHandler(Add);
IAsyncResult result = handler.BeginInvoke(1,2,null,null);
Console.WriteLine("Do other work... ... ...");
Console.WriteLine(handler.EndInvoke(result));
Console.ReadLine();
}
static int Add(int a, int b) {
Console.WriteLine("Computing "+a+" + "+b+" ...");
Thread.Sleep(3000);
Console.WriteLine("Computing Complete.");
return a+b;
}
}运行结果: **********AsyncInvokeTest**************
Do other work... ... ...
Computing 1 + 2 ...
Computing Complete.
可以看到,主线程并没有等待,而是直接向下运行了。
但是问题依然存在,当主线程运行到EndInvoke时,如果这时调用没有结束(这种情况很可能出现),这时为了等待调用结果,线程依旧会被阻塞。
解决的办法是用回调函数,当调用结束时会自动调用回调函数
回调异步:
public class Foo {
static void Main() {
Console.WriteLine("**********AsyncInvokeTest**************");
AddHandler handler = new AddHandler(Add);
IAsyncResult result = handler.BeginInvoke(1,2,new AsyncCallback(AddComplete),"AsycState:OK");
Console.WriteLine("Do other work... ... ...");
Console.ReadLine();
}
static int Add(int a, int b) {
Console.WriteLine("Computing "+a+" + "+b+" ...");
Thread.Sleep(3000);
Console.WriteLine("Computing Complete.");
return a+b;
}
static void AddComplete(IAsyncResult result) {
AddHandler handler = (AddHandler)((AsyncResult)result).AsyncDelegate;
Console.WriteLine(handler.EndInvoke(result));
Console.WriteLine(result.AsyncState);
}
}
标签:C#,同步调用,异步调用
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
c# 接口interface基础入门小例子
2023-11-04 16:55:45
java获取网络图片上传到OSS的方法
2023-10-14 23:01:07
Java 数据结构与算法系列精讲之环形链表
2023-04-27 22:37:07
![](https://img.aspxhome.com/file/2023/2/81362_0s.gif)
Java生产1-100的随机数简单实例(分享)
2021-08-15 02:52:28
java计算给定字符串中出现次数最多的字母和该字母出现次数的方法
2022-01-13 03:00:04
C++ 中const修饰虚函数实例详解
2022-01-06 16:40:47
springboot多模块化整合mybatis,mapper自动注入失败问题及解决
2022-02-20 08:06:29
![](https://img.aspxhome.com/file/2023/8/60778_0s.jpg)
实例化JFileChooser对象报空指针异常问题的解决办法
2023-10-05 11:36:18
![](https://img.aspxhome.com/file/2023/7/132067_0s.png)
win10和win7下java开发环境配置教程
2022-05-01 05:34:22
![](https://img.aspxhome.com/file/2023/6/79866_0s.png)
android开机自启动app示例分享
2023-02-20 18:04:29
Java并发编程示例(一):线程的创建和执行
2022-01-24 16:43:49
JAVA熔断和降级真实关系的图文详解
2023-11-30 12:39:20
![](https://img.aspxhome.com/file/2023/7/67957_0s.png)
Java中IO流解析及代码实例详解
2022-03-08 22:32:16
C语言 auto和register关键字
2021-11-03 02:40:54
![](https://img.aspxhome.com/file/2023/1/107401_0s.png)
Android 关闭多个Activity的实现方法
2022-02-24 21:21:19
Java 定时任务技术趋势详情
2021-10-29 14:48:13
![](https://img.aspxhome.com/file/2023/0/96070_0s.webp)
Java中Steam流的用法详解
2021-12-16 14:18:50
Android利用Flutter实现立体旋转效果
2023-06-20 08:20:32
![](https://img.aspxhome.com/file/2023/9/96759_0s.png)
Android自定义个性化的Dialog示例
2022-01-21 12:41:36
详解Java中的final关键字
2023-02-20 17:29:50