异步未按预期工作 [英] Async not working as I expected

查看:87
本文介绍了异步未按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了我在asp.net(c#)中的异步编程中可以找到的所有内容.我已经了解了它应该如何工作以及何时应该使用它的大部分内容.但是,我发现基本示例无法按我预期的那样工作.如果没有 Task.Run ,它似乎实际上并不会异步运行.

有人可以告诉我此示例中我缺少什么吗?

说代码就像这样

 公共异步任务SubTask2(){LongRunningOperation2();Response.Write(< br> ------------------------已完成-------------------------< br>);}私有异步Task< Boolean>LongRunningOperation1(){整数计数器for(计数器= 0;计数器<50000;计数器++){Response.Write(counter +< br>");}返回等待Task.FromResult< bool>(true);}私有异步Task< Boolean>LongRunningOperation2(){等待LongRunningOperation1();Response.Write(< br> ------------------------长任务-------------------------< br>);返回true;} 

LongRunningOperation2()是否应该返回到 SubTask2()并在写数字之前或同时打印完成"?而是在末尾打印完成.使用 Task.Run 可以按预期工作,但是我看不到从未使用 Task.Run

的意义.

解决方案

async / await 模式的设计考虑因素是,有时代码具有async 签名可能会同步(立即)返回-可能是由于缓存或本地数据缓冲(可能是从套接字读取数据,可能还有剩余数据要从缓冲区中使用),或者可能是由于IoC等提供了异步签名的同步实现.在那种情况下,整个引擎被设计为通过作为回调进行任何优化,而是继续同步运行.这不是一个极端的情况-最近的C#更新通过添加对自定义等待对象(特别是: ValueTask< T> )的支持来扩展,以使做到这一点 的条件下,效率提高.

异步的目的是促进具有真正异步组件的场景,与等待异步操作完成相比,释放线程做更多有用的事情.关于并行化不是不是.

在您的情况下,您的所有代码实际上都是同步的,因此它将一直一直同步运行.

I've read all I can find on async programming in asp.net (c#). I've made sense of most of how it's supposed to work and when it should be used. Yet I find basic examples not working as I expect. Without Task.Run it doesn't seem to actually run asynchronously.

Can someone tell me what I'm missing in this example?

Say the code is like so

public async Task SubTask2()
{
    LongRunningOperation2();
    Response.Write("<br>------------------------ Finished -------------------------<br>");
}

private async Task<Boolean> LongRunningOperation1()
{
    int counter;

    for (counter = 0; counter < 50000; counter++)
    {
        Response.Write(counter + "<br>");
    }
    return await Task.FromResult<bool>(true);
}

private async Task<Boolean> LongRunningOperation2()
{
    await LongRunningOperation1();
    Response.Write("<br>------------------------ Long Task -------------------------<br>");
    return true;
}

Shouldn't LongRunningOperation2() return to SubTask2() and print "finished" before or while writing out numbers? Instead it prints finished at the end. Using Task.Run works as expected but then I don't see the point of ever not using Task.Run

解决方案

A design consideration of the async/await pattern is that sometimes code that has an async signature might return synchronously (immediately) - perhaps due to caching or local data buffering (reading data from a socket, perhaps, and having spare data left to consume from the buffer), or perhaps due to IoC etc providing a synchronous implementation of an asynchronous signature. In that scenario, the entire engine is designed to optimize by not doing anything as callbacks, but continuing to run synchronously. This is not an edge-case - recent C# updates have extended this by adding support for custom awaitables (in particular: ValueTask<T>) to make this even more efficient in the case when something completes with a synchronous but non-trivial result.

The purpose of async is to facilitate scenarios that have genuinely async components, freeing up the thread to do more useful things than waiting for an async operation to complete. It is not about parallelization.

In your case, all of your code is actually synchronous, so it continues to run synchronously all the way through.

这篇关于异步未按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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