调用使用Task.Run异步方法似乎是错误的? [英] Calling an async method using a Task.Run seems wrong?

查看:171
本文介绍了调用使用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.net,你有一个的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.net 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异步方法似乎是错误的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆