当前位置:网站首页>同步方法中不使用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();
}
边栏推荐
- Review of database -- 3. SQL language
- 【Halcon视觉】算子的结构
- What will the new Fuzhou Xiamen railway bring to Fujian coastal areas?
- [Halcon vision] programming logic
- 微信公众号发布提醒(微信公众号模板消息接口)
- Navicat15连接本地虚拟机的Mysql(Centos7)
- 【socket】三次握手是在listen中完成,accept只从完成连接的队列中拿出一个连接
- Interview questions and answers of the first company (I)
- Using native JS to realize custom scroll bar (click to reach, drag to reach)
- 【Halcon视觉】形态学膨胀
猜你喜欢
随机推荐
.NET操作Redis sorted set有序集合
The problem of incomplete or partial display of the last recyclerview is solved
数据库的复习--3.SQL语言
MLX90640 红外热成像仪测温传感器模块开发笔记(六)红外图像伪彩色编码
[socket] the three handshakes are completed in listen, and accept only takes out one connection from the queue that completes the connection
Study on the basis of opencv
Kaptcha image verification code integration
分布式锁解决方案之Redis实现
数据库函数
The difference between equals and = =
Agenda express | list of sub forum agenda on July 27
Data communication foundation telnet remote management equipment
【C#语言】具名类型和匿名类型
新建福厦铁路全线贯通 这将给福建沿海带来什么?
Learning about tensorflow (II)
L2-005 集合相似度(vector、set求并交集)
404页面和路由钩子
Android greendao数据库的使用
[C language] named type and anonymous type
Draco developed by Google and Pixar supports USD format to accelerate 3D object transmission & lt; Forward & gt;

![[Halcon vision] morphological expansion](/img/ce/abaca036fce5b67dfe6ac361aecfea.png)

![[Halcon vision] affine transformation](/img/f1/32284c71e78e6eea390fdb6058ba0f.png)



![[Halcon vision] programming logic](/img/1a/b6daac946fbefd8337355dc8b7873e.png)
