当前位置:网站首页>Asynctask < T> decoration and await are not used in synchronous methods to obtain asynchronous return values (asynchronous methods are called in synchronous methods)
Asynctask < T> decoration and await are not used in synchronous methods to obtain asynchronous return values (asynchronous methods are called in synchronous methods)
2022-07-26 10:33:00 【Miners learn programming】
In development , Our common asynchronous call is to add async Task<T> , Specific calls are used on asynchronous methods var value = await funtion(), To get the value .
It is often written like this .
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);
}But sometimes , Method Index Methods can only be synchronized , How should I call it ? It's very simple , Just add .Result That's all right. .
public IActionResult Index()
{
var client = new HttpClient(new HttpClientHandler
{
CookieContainer = new CookieContainer(),
UseCookies = true
});
var str = client.GetStringAsync("youUrl").Result;
return Content(str);
} Call asynchronous method in synchronous method ( No return value ) reference :C# Call asynchronous method in synchronous method This blog
static void Main(string[] args)
{
TestAsync().Wait();
}
边栏推荐
猜你喜欢

Dynamically determine file types through links

干货likeshop外卖点餐系统开源啦100%开源无加密

【Halcon视觉】编程逻辑
![[Halcon vision] image filtering](/img/4b/e73a8d589b49276d96621f0ef02449.png)
[Halcon vision] image filtering

单元测试,到底什么是单元测试,为什么单测这么难写
![[Halcon vision] morphological corrosion](/img/f9/f01dd3340824ff28c84cf7bb52882e.png)
[Halcon vision] morphological corrosion

QRcode二维码(C语言)遇到的问题

PLC概述

Redis Docker实例与数据结构

Introduction to data analysis | kaggle Titanic mission (I) - > data loading and preliminary observation
随机推荐
Function template parameters (where are the function parameters)
Analyze the hybrid construction objects in JS in detail (construction plus attributes, prototype plus methods)
.NET操作Redis Set无序集合
PLC概述
[Halcon vision] Fourier transform of image
js翻页、kkpager.js翻页
Tradingview 使用教程
事务的传播性propagation
Redis Docker实例与数据结构
简单化构造函数的继承方法(二)- ES6中的class继承
[Halcon vision] image filtering
Summary of common skills in H5 development of mobile terminal
【Halcon视觉】图像灰度变化
抓包工具fiddler和wireshark对比
Introduction to data analysis | kaggle Titanic mission (I) - > data loading and preliminary observation
单元测试,到底什么是单元测试,为什么单测这么难写
Our Web3 entrepreneurship project is yellow
数据分析入门 | kaggle泰坦尼克任务(一)—>数据加载和初步观察
Comparison of packet capturing tools fiddler and Wireshark
Listening freely, the next stop of online text traffic competition?