当前位置:网站首页>同步方法中不使用asyncTask<T> 修饰和await获取异步返回值(同步方法中调用异步方法)
同步方法中不使用asyncTask<T> 修饰和await获取异步返回值(同步方法中调用异步方法)
2022-07-26 10:29:00 【矿工学编程】
实际开发中,我们常见的异步调用就是在调用的方法上加上 async Task<T> ,具体调用异步的方法上使用 var value = await funtion(),去获取值。
常见的就是这么写。
public async Task<IActionResult> Index()
{
var client = new HttpClient(new HttpClientHandler
{
CookieContainer = new CookieContainer(),
UseCookies = true
});
var str = await client.GetStringAsync("youUrl");
return Content(str);
}但是有时候,方法Index方法只能同步,应该怎么调用呢?其实非常简单,只要在具体需要调用的异步方法后面加.Result就可以了。
public IActionResult Index()
{
var client = new HttpClient(new HttpClientHandler
{
CookieContainer = new CookieContainer(),
UseCookies = true
});
var str = client.GetStringAsync("youUrl").Result;
return Content(str);
}同步方法中调用异步方法(无返回值)参照:C#同步方法中调用异步方法 这篇博客
static void Main(string[] args)
{
TestAsync().Wait();
}
边栏推荐
- 并行、并发及对于高并发优化的几个方向
- [Halcon vision] programming logic
- 【Halcon视觉】编程逻辑
- 记给esp8266烧录刷固件
- 【杂谈】Error loading psycopg2 module :No module named psycopg2
- 【Halcon视觉】阈值分割
- [C language] named type and anonymous type
- 移动端H5开发常用技巧总结
- Employee information management system based on Web
- 头歌 Phoenix 入门(第1关:Phoenix 安装、第2关:Phoenix 基础语法)
猜你喜欢
随机推荐
移动端双指缩放事件(原生),e.originalEvent.touches
事务的传播性propagation
2022pta平时训练题(1~10题字符串处理问题)
cavans实现静态滚动弹幕
Review of database -- 1. Overview
Analyze the hybrid construction objects in JS in detail (construction plus attributes, prototype plus methods)
Modelsim installation tutorial (application not installed)
Mlx90640 infrared thermal imager temperature sensor module development notes (6)
Redis realizes the correct posture of token bucket
议程速递 | 7月27日分论坛议程一览
About automatic operation on Web pages
Okaleido生态核心权益OKA,尽在聚变Mining模式
SAP ABAP Netweaver 容器化的一些前沿性研究工作分享
Redis realizes distributed lock and gets a watchdog
[Halcon vision] image filtering
【Halcon视觉】图像滤波
Learning about opencv (1)
Learning about opencv (4)
MD5加密
[Halcon vision] polar coordinate transformation




![[Halcon vision] software programming ideas](/img/9b/a27338689ee4598dac88f6e5d92053.png)
![[Halcon vision] threshold segmentation](/img/1c/e2463a796f99804a55680b69e714a6.png)

