当前位置:网站首页>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();
}
边栏推荐
- Google与Pixar开发Draco支持USD格式 加速3D对象传输&lt;转发&gt;
- What is wrong about the description of function templates (how to solve link format errors)
- [Halcon vision] affine transformation
- 卸载魅族应用商店
- The problem of incomplete or partial display of the last recyclerview is solved
- Structure of [Halcon vision] operator
- json-c库的简单使用——将json文件转换为struct.
- 利用原生js实现自定义滚动条(可点击到达,拖动到达)
- 常见的类(了解)
- uniapp使用简单方法signalR(仅用于web调试,无法打包app)
猜你喜欢

数据分析入门 | kaggle泰坦尼克任务(一)—>数据加载和初步观察

【Halcon视觉】阈值分割

Deduct daily question 838 of a certain day

Li Kou daily question 917

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

PLC overview

【dectectron2】跟着官方demo一起做

Okaleido生态核心权益OKA,尽在聚变Mining模式

Unit test, what is unit test and why is it so difficult to write a single test

【Halcon视觉】形态学膨胀
随机推荐
数据分析入门 | kaggle泰坦尼克任务(一)—>数据加载和初步观察
About the declaration and definition of template functions [easy to understand]
【Halcon视觉】数组
Agenda express | list of sub forum agenda on July 27
记给esp8266烧录刷固件
Using native JS to realize custom scroll bar (click to reach, drag to reach)
移动端H5开发常用技巧总结
Introduction to Phoenix (Level 1: Phoenix installation, level 2: Phoenix basic grammar)
json_object_put: Assertion `jso->_ref_count > 0‘ failed.Aborted (core dumped)
Our Web3 entrepreneurship project is yellow
.NET操作Redis List列表
干货likeshop外卖点餐系统开源啦100%开源无加密
The problem of incomplete or partial display of the last recyclerview is solved
【Halcon视觉】编程逻辑
3.1 leetcode daily question 6
移动端双指缩放事件(原生),e.originalEvent.touches
结构体操作报错:Segmentation fault (core dumped)
Navicat15 MySQL (centos7) connected to local virtual machine
数据库函数
畅听,网文流量竞争的下一站?