首页 > 其他分享 >'await' works, but calling task.Result hangs/deadlocks

'await' works, but calling task.Result hangs/deadlocks

时间:2023-03-09 16:46:12浏览次数:39  
标签:Task Run thread await hangs calling task Result async

'await' works, but calling task.Result hangs/deadlocks

回答1

Acquiring a value via an async method:

var result = Task.Run(() => asyncGetValue()).Result;

Syncronously calling an async method

Task.Run( () => asyncMethod()).Wait();

No deadlock issues will occur due to the use of Task.Run.

 

回答2

You're running into the standard deadlock situation that I describe on my blog and in an MSDN article: the async method is attempting to schedule its continuation onto a thread that is being blocked by the call to Result.

In this case, your SynchronizationContext is the one used by NUnit to execute async void test methods. I would try using async Task test methods instead.

 

回答3

An addition to the answer given by @HermanSchoenfeld. Unfortunately the quote below is not true:

No deadlock issues will occur due to the use of Task.Run.

public String GetSqlConnString(RubrikkUser user, RubrikkDb db) 
{ 
    // deadlock if called from threadpool, 
    // works fine on UI thread, works fine from console main 
    return Task.Run(() => 
        GetSqlConnStringAsync(user, db)).Result; 
}

The execution is wrapped inside a Task.Run, this will schedule the task on the threadpool the block the calling thread. This is okay, as long as the calling thread is not a threadpool thread. If the calling thread is from the threadpool then the following disaster happens: A new task is queued to the end of the queue, and the threadpool thread which would eventually execute the Task is blocked until the Task is executed.

In library code there is no easy solution as you cannot assume under what context your code is called. The best solution is to only call async code from async code, blocking sync APIs from sync methods, don’t mix them.

Source:

https://medium.com/rubrikkgroup/understanding-async-avoiding-deadlocks-e41f8f2c6f5d

 

标签:Task,Run,thread,await,hangs,calling,task,Result,async
From: https://www.cnblogs.com/chucklu/p/17199030.html

相关文章

  • ES6-ES11 async与await结合读取文件内容
    原视频//1.引入fs模块constfs=require("fs");//读取『为学』functionreadWeiXue(){returnnewPromise((resolve,reject)=>{fs.readFile(".......
  • 中断下文tasklet、工作队列
    tasklettasklet结构体structtasklet_struct{};unsignedlongdata还可以区分tasklettasklet相关函数示例核心代码......
  • celery task 异常捕获的两种方式.(sentry, mail)
    捕获celerytask异常的两种方式,sentry,mailsentry(sentry记录异常.)importsentry_sdkfromsentry_sdk.integrations.celeryimportCeleryIntegrationsentry_......
  • 第126篇: 异步函数(async和await)
    好家伙,本篇为《JS高级程序设计》第十章“期约与异步函数”学习笔记 ES8的async/await旨在解决利用异步结构组织代码的问题。为为此增加了两个新关键字:async和awa......
  • Spring Task
    SpringTask定时任务1.主要内容 2.定时任务概述在项目中开发定时任务应该一种比较常见的需求,在Java中开发定时任务主要有三种解决方案:一是使用JDK自带的Timer......
  • C# 多线程(Thread和Task)
    C#多线程(Thread和Task)线程(Thread)是进程中的基本执行单元,是操作系统分配CPU时间的基本单位,一个进程可以包含若干个线程,在进程入口执行的第一个线程被视为这个进......
  • C# 异步编程(async和await)
    C#异步编程(async和await)在C#中,如果需要I/O绑定(例如从网络请求数据、访问数据库或读取和写入到文件系统),则需要利用异步编程。还可以使用CPU绑定代码(例如执......
  • 这样在 C# 使用 LongRunnigTask 是错的
    Task.Factory.StartNew有一个重载,是支持TaskCreationOptions.LongRunning参数来指定Task的特征的。但是可能在没有注意的情况下,你就使用了错误的用法。那么本文我们来......
  • task7.c
    代码#include<stdio.h>#include<stdlib.h>intmain(){ intn; n=rand()%100-60+1+60; printf("n=%d\n",n); return0;}图片 ......
  • task6.c
    代码#include<stdio.h>intmain(){ intyear,s; longdoublek; s=1e+9; k=s/60/60/24/365; year=(int)(k+0.5); printf("十亿秒约等于%d年\n",year);......