当前位置: 首页 > news >正文

C#使用异步方式调用同步方法的实现方法

使用异步方式调用同步方法,在此我们使用异步编程模型(APM)实现
1、定义异步委托和测试方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;namespace ApmAsyncApp
{public delegate string TestMethodAsyncCaller(int callDuration, out int threadId);/// <summary>internal class AsyncDemo{/// 执行异步调用的方法/// </summary>/// <param name="callDuration"></param>/// <param name="threadId"></param>/// <returns></returns>public string TestMethod(int callDuration,out int threadId){Console.WriteLine("Test method begins:");Thread.Sleep(callDuration);threadId=Thread.CurrentThread.ManagedThreadId;return String.Format("Call time was {0}",callDuration);}}
}

2、使用 EndInvoke 等待异步调用

static void Main(string[] args){int threadId;//创建测试类AsyncDemo asyncDemo = new AsyncDemo();//创建委托TestMethodAsyncCaller caller = new TestMethodAsyncCaller(asyncDemo.TestMethod);//初始化异步调用IAsyncResult result = caller.BeginInvoke(3000, out threadId, null, null);Thread.Sleep(0);Console.WriteLine("Main thread {0} does some work.", Thread.CurrentThread.ManagedThreadId);//调用EndInvoke检索结果string returnValue = caller.EndInvoke(out threadId, result);Console.WriteLine("The call executed on thread {0},with return value \"{1}\".", threadId, returnValue);Console.ReadLine();}

在这里插入图片描述
3、使用 WaitHandle 等待异步调用

static void Main(string[] args){int threadId;//创建测试类AsyncDemo asyncDemo = new AsyncDemo();//创建委托TestMethodAsyncCaller caller = new TestMethodAsyncCaller(asyncDemo.TestMethod);//初始化异步调用IAsyncResult result = caller.BeginInvoke(3000, out threadId, null, null);Thread.Sleep(0);Console.WriteLine("Main thread {0} does some work.", Thread.CurrentThread.ManagedThreadId);//sone to do//等待信号result.AsyncWaitHandle.WaitOne();//调用EndInvoke检索结果string returnValue = caller.EndInvoke(out threadId, result);result.AsyncWaitHandle.Close();Console.WriteLine("The call executed on thread {0},with return value \"{1}\".", threadId, returnValue);Console.ReadLine();}

在这里插入图片描述
4、对异步调用的完成情况进行轮询

static void Main(string[] args){int threadId;//创建测试类AsyncDemo asyncDemo = new AsyncDemo();//创建委托TestMethodAsyncCaller caller = new TestMethodAsyncCaller(asyncDemo.TestMethod);//初始化异步调用IAsyncResult result = caller.BeginInvoke(3000, out threadId, null, null);Thread.Sleep(0);Console.WriteLine("Main thread {0} does some work.", Thread.CurrentThread.ManagedThreadId);// 等待完成.while (result.IsCompleted == false){Thread.Sleep(250);Console.Write(".");}//调用EndInvoke检索结果string returnValue = caller.EndInvoke(out threadId, result);Console.WriteLine("The call executed on thread {0},with return value \"{1}\".", threadId, returnValue);Console.ReadLine();}

在这里插入图片描述
5、异步调用完成时执行回调方法

static void Main(string[] args){int threadId = 0;//创建测试类AsyncDemo asyncDemo = new AsyncDemo();//创建委托TestMethodAsyncCaller caller = new TestMethodAsyncCaller(asyncDemo.TestMethod);//初始化异步调用IAsyncResult result = caller.BeginInvoke(3000, out threadId, new AsyncCallback(callbackMethod), "The call executed on thread {0},with return value \"{1}\".");Console.WriteLine("Main thread {0} does some work.", Thread.CurrentThread.ManagedThreadId);Thread.Sleep(5000);Console.WriteLine("The main thread ends.");Console.ReadLine();}private static void callbackMethod(IAsyncResult ar){//检索委托.AsyncResult result = (AsyncResult)ar;TestMethodAsyncCaller caller = (TestMethodAsyncCaller)result.AsyncDelegate;//格式字符串作为状态信息被传递string formatString = (string)ar.AsyncState;int threadId = 0;//调用EndInvoke检查结果.string returnValue = caller.EndInvoke(out threadId, ar);//用格式字符串格式化输出信息Console.WriteLine(formatString, threadId, returnValue);}

在这里插入图片描述

http://www.lryc.cn/news/398288.html

相关文章:

  • 【Go系列】 Go语言的入门
  • Dify 与 Xinference 最佳组合 GPU 环境部署全流程
  • MICCAI 2024Centerline Boundary Dice Loss for Vascular Segmentation
  • golang验证Etherscan上的智能合约
  • Visual Studio编译优化选项
  • sql业务场景分析思路参考
  • Django权限系统如何使用?
  • 基于整体学习的大幅面超高分遥感影像桥梁目标检测(含数据集下载地址)
  • 逻辑回归模型(非回归问题,而是解决二分类问题)
  • QT的OpenGL渲染窗QOpenGLWidget Class
  • 单元测试和集成测试
  • 【JAVA入门】Day15 - 接口
  • ES6 之 Set 与 Map 数据结构要点总结(一)
  • 一文学会用RKE部署高可用Kubernetes集群
  • 数据加密的常见方法
  • 天童美语:推荐给孩子的人文历史纪录片
  • 数字人技术如何推动教育事业可持续创新发展?
  • FPGA程序设计
  • 彻底开源,免费商用,上海AI实验室把大模型门槛打下来
  • MTEB评估基准使用指北
  • 31. 1049. 最后一块石头的重量 II, 494.目标和,474.一和零
  • PDF 中图表的解析探究
  • 递推(C语言)
  • 安卓微信8.0之后如何利用缓存找回的三天之前不可见的朋友圈图片
  • ES6 Class(类) 总结(九)
  • 使用 Vue.js 和 Element Plus 实现自动完成搜索功能
  • SpringBoot自定义starter
  • 深入探索大语言模型
  • querylist多线程采集curlMulti时,报错Curl error(60)
  • Python数据分析~~美食排行榜