问题描述
原谅我,如果这是一个愚蠢的问题。我已经通过MSDN文档整天被浇,他们的异步编码理念是混淆了我。据我了解,调用异步方法的线程不会,如果异步方法被称为阻塞。然而,异步总是成对的例子用的await,这似乎否定了异步的烦躁,使得它使得外部方法是否有等待代码执行反正。我不应该能够调用一个异步方法,然后与外部方法的执行下去?
Forgive me if this is a stupid question. I've been pouring through MSDN docs all day, and their philosophy of asynchronous coding is confusing me. As I understand it, the thread that calls the async method will not be blocked if the async method is called. Yet, async is always paired in examples with await, which appears to negate the async-ness, making it so that the outer method DOES have to wait for the code to execute anyway. Shouldn't I be able to call an async method and then continue with the execution of the outer method?
这是我一直遇到的情况,或多或少:
This is the scenario I've been encountering, more or less:
void reportSomethingHappened(info)
- Collect info
- HTTP POST info to logging server (ie. mixpanel, sentry)
这将是一个调用方法:
void largerProcess
if (whatever)
reportSomethingHappened();
bla;
bla;
据我了解,因为POST请求可以异步完成的,我应该能够使reportSomethingHappened ()为异步方法(按,据我所知,待机-ING将向WebRequest,并添加async关键字)。
As far as I understand, since POST requests can be done asynchronously, I should be able to make reportSomethingHappened() into an async method (by, AFAIK, await-ing the webrequest, and adding the async keyword).
但largerProcess方法并不需要等待(即等候)报告的方法,以便执行喇嘛喇嘛来完成。然而,VS告诉我,与异步方法我可以等待它,或者它会同步发生,和块。难道这不是打败单独做的目的是什么?
But the largerProcess method doesn't need to wait for (ie. await) the reporting method to finish in order to execute bla bla. Yet, VS tells me that with an async method I can either await it, or it will happen synchronously, and block. Doesn't that defeat the purpose of doing it separately?
我怎样写largerProcess这使reportSomethingHappened不会阻止执行? (这本质上是困惑我,因为我认为这是异步的点都一起)
How do I write this so that reportSomethingHappened doesn't block execution of largerProcess? (Which is inherently confusing me, because I thought that was the point of async all along)
推荐答案
如果你调用一个异步方法这将异步你是否等待
返回的任务或无法运行。
If you call an asynchronous method it will run asynchronously whether you await
the returned task or not.
的await
不影响如何执行的方法,只是你如何与它的调用者的交易。您可以调用异步方法,获得任务,等待它的时候了(这是最简单的选项)。这将使你写代码的看起来的同步,但同步运作,为等待
之后就基本注册代码的其余部分作为回调被执行只有在等待任务完成。这并不在传统的方框,因为因为没有线程被阻塞,但代码流将是连续的:
await
doesn't affect how the method executes, only how you as the caller deals with it. You can call the async method, get the task and await it right away (which is the simplest option). That will enable you to write code that looks synchronous but runs asynchronously as await
basically registers the rest of the code after it as a callback to be executed only after the awaited task is completed. This doesn't block in the traditional since as no thread is blocked, but the code flow will be sequential:
async Task LargerProcessAsync()
{
if (condition)
{
await ReportSomethingHappenedAsync();
}
// do other stuff
}
不过,你并不绝对需要做到这一点。你可以得到任务回来,做其他的东西,然后才等待
是:
However, you don't absolutely need to do that. You can get the task back, do other stuff and only then await
it:
async Task LargerProcessAsync()
{
Task task = null;
if (condition)
{
task = ReportSomethingHappenedAsync();
}
// do other stuff
if (task != null)
{
await task;
}
}
或者你可以简单地删除等待
完全。你应该明白,虽然,这可能是危险的,因为该任务可以出现故障和异常可以去不可观测的,这就是为什么它气馁。有几种方法可以做到这没错,但他们并不简单。您可以使用 Task.ContinueWith
:
Or you can simply remove the await
completely. You should realize though that this can be dangerous as the task can be faulted and the exception can go unobserved and that's why it's discouraged. There are several ways to do this right, but they aren't simple. You can use Task.ContinueWith
:
void LargerProcess()
{
if (condition)
{
ReportSomethingHappenedAsync().ContinueWith(task =>
{
try
{
task.Wait();
}
catch (Exception exception)
{
// handle exception
}
})
}
// do other stuff
}
或者用于ASP.Net看看射后不理的ASP.NET 一>
Or for ASP.Net look at Fire and Forget on ASP.NET
这篇关于为什么我必须使用等待的方法来异步运行。如果我不想等待的方法来继续前完成什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!