解析C#中委托的同步调用与异步调用(实例详解)
- 作者: 倾尽一世柔情丶
- 来源: 51数据库
- 2021-10-19
委托的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);
}
}
同步调用的例子:
复制代码 代码如下:
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#通过fleck实现wss协议的WebSocket多人Web实时聊天(附源码)
- 团队城市未满足要求:MSBuildTools12.0_x86_Path 存在
- 使用 MSBuild.exe 在发布模式下构建 C# 解决方案
- 当我发布 Web 应用程序时,AfterPublish 脚本不运行
- 构建时 T4 转换的产品仅在下一个构建中使用
- ASP.NET Core Application (.NET Framework) for Windows x64 only error in project.assets.json
- 新的 .csproj 格式 - 如何将整个目录指定为“链接文件"到子目录?
- 如何将条件编译符号(DefineConstants)传递给 msbuild
- MSBuild 支持 Visual Studio 2017 RTM 中的 T4 模板
- NuGet 包还原找不到包,没有源
热点文章
团队城市未满足要求:MSBuildTools12.0_x86_Path 存在
0
使用 MSBuild.exe 在发布模式下构建 C# 解决方案
0
当我发布 Web 应用程序时,AfterPublish 脚本不运行
0
构建时 T4 转换的产品仅在下一个构建中使用
0
ASP.NET Core Application (.NET Framework) for Windows x64 only error in project.assets.json
0
新的 .csproj 格式 - 如何将整个目录指定为“链接文件"到子目录?
0
如何将条件编译符号(DefineConstants)传递给 msbuild
0
MSBuild 支持 Visual Studio 2017 RTM 中的 T4 模板
0
NuGet 包还原找不到包,没有源
0
使用 C# 6.0 功能运行 TFS 构建
0