调用使用Task.Run异步方法似乎是错误的?

编程入门 行业动态 更新时间:2024-10-26 14:35:38
本文介绍了调用使用Task.Run异步方法似乎是错误的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我最近在由我们已经为我们工作的承包商写成这样code凸轮。这是不是过分地聪明或愚蠢的(我认为后者,但我想第二个意见)。我没有大量的加快速度异步 等待。

基本上,它的工作是这样的:

公共BOOL发送(TemplatedMessageDto消息){    返回Task.Run(()=> SendAsync(消息))        .GetAwaiter()        .GetResult();}公共异步任务<布尔> SendAsync(TemplatedMessageDto消息){    // code做的东西    VAR的结果=等待_externalresource.DothingsExternally();    // code做的东西}

现在我的理解是,首先 Task.Run()是没有意义的,效率低下?而真正应该:

公共BOOL发送(TemplatedMessageDto消息){    返回SendAsync(消息))    .GetAwaiter()    .GetResult();}公共异步任务<布尔> SendAsync(TemplatedMessageDto消息){    // code做的东西    VAR的结果=等待_externalresource.DothingsExternally();    // code做的东西}

我也是不相信这是真的异步方法,因为它仍然会等待,对不对?我认为这是唯一的优势(甚至重新编写)是释放的主要工作线程。

有人可以证实,这第一个任务不应该存在?

解决方案   

我也是不相信这是真的异步方法,因为它仍然会等待,对吧?

这是不是像尤瓦解释。你不应该使用同步异步以上。

  

现在我的理解是,首先 Task.Run()是没有意义的,效率低下?

不是真的,但在以这样的方式使用 Task.Run 值。

既然你阻塞异步方法(你不应该这样做)有,你会僵局的机会。这发生在UI应用程序和asp,你有一个的SynchronizationContext 。

使用 Task.Run 可以清除的SynchronizationContext ,因为它卸载工作到线程池线程和删除死锁的风险。

所以,拦网不好,但如果你最终用做 Task.Run 更安全。

I recently cam across this code written by a contractor we had working for us. It's either devilishly clever or silly (I think the latter but I wanted a second opinion). I'm not massively up to speed on async await.

Basically it worked like this:

public bool Send(TemplatedMessageDto message) { return Task.Run(() => SendAsync(message)) .GetAwaiter() .GetResult(); } public async Task<bool> SendAsync(TemplatedMessageDto message) { //code doing stuff var results = await _externalresource.DothingsExternally(); //code doing stuff }

Now as I understand it that first Task.Run() is pointless and inefficient? and should really be:

public bool Send(TemplatedMessageDto message) { return SendAsync(message)) .GetAwaiter() .GetResult(); } public async Task<bool> SendAsync(TemplatedMessageDto message) { //code doing stuff var results = await _externalresource.DothingsExternally(); //code doing stuff }

I'm also not convinced this is really an async method because it will still wait, right? I think it's only advantage (even re-written) is to free up the main worker thread.

Can someone confirm that this first Task shouldn't be there?

解决方案

I'm also not convinced this is really an async method because it will still wait, right?

It isn't, as Yuval explained. You shouldn't use sync over async.

Now as I understand it that first Task.Run() is pointless and inefficient?

Not really, there is value in using Task.Run in such a way.

Since you're blocking on an async method (which you shouldn't do) there is a chance you'll deadlock. That happens in UI apps and asp where you have a SynchronizationContext.

Using Task.Run clears that SynchronizationContext since it offloads the work to a ThreadPool thread and removes the risk for a deadlock.

So, blocking is bad, but if you end up doing it using Task.Run is safer.

更多推荐

调用使用Task.Run异步方法似乎是错误的?

本文发布于:2023-11-03 07:47:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1554604.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:似乎是   错误   方法   Task   Run

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!