问题描述
我想了解如何在C#中伺机异步工作,有一件事是混淆了我很多。我的理解是,使用关键字等待任何方法必须以异步标记。
我的理解是,当与等待关键字线被击中不执行该行低于code。异步操作开始进行中的await行语句,并且控制返回到可以执行继续调用方法。
I am trying to understand how await async work in C# and one thing is confusing me a lot. I understand that any method that uses await keyword must be marked with async.My understanding is that when a line with await keyword is hit the code below that line is not executed. An async operation is started to carry on the statement in the await line and the control is returned to the calling method which can proceed with the execution.
问题1:这是假设是正确的或仍在执行的await关键字低于code
Question # 1: Is this assumption correct or the code below the await keyword is still executed?
其次假设我叫了服务方法和异步需要返回它的结果。 return语句是等待关键字的下面。
Secondly suppose I called a service method async and need to return its result. The return statement is below the await keyword.
问题2:什么时候是return语句命中,异步调用完成后或之前
Question # 2: When is the return statement hit, after the async call has completed or before it?
问题3:我想使用该服务调用和异步操作的结果不会帮助,因为我想在已经返回的结果被打调用方法。我明白这可以通过使用Result属性,这使得呼叫SYNCHRONUS来完成。那么是什么在DB的操作使用异步的原因,他们是实际需要的80%的时间在大多数应用程序的人。
Question # 3: I want to use the result of that service call and async operation wont help cause I want the calling method to be hit when the result has been returned. I understand this can be done using the Result property which makes the call synchronus. But then what is the use of async in DB operations cause they are the ones that actually take 80% of the time in most apps.
问题4:我如何使用异步操作DB使用?是否有可能和recomended?
Question # 4: How can I use async with DB operations? Is it possible and recomended?
问题5:在这scenraio将异步操作是有益的,似乎每个API只是做异步操作现在没有理由吗?还是我错过使用异步OPS点?
Question # 5: In which scenraio will async operations be useful it seems that every api is just making async operations now without a reason? or did I miss the point of using async ops?
我的意思是说,API正在ASYN方法没有一个原因是因为方法必须返回的东西,直到该计算是由什么,他们如何在本质论返回因此不会调用还处于阻塞感,它会直到返回的结果是无用的?
What I mean by saying that api are making asyn methods without a reason is because methods have to return something and untill that calculation is made how can they return so in essense won't the call be still blocking in sense that it will be useless until result is returned?
推荐答案
MSDN的。
据我了解,虽然有时香草文档(特别是来自MSDN)可能难以适用于您的具体情况,让我们去了你的观点。
I understand though that sometimes vanilla docs (particularly from MSDN) can be difficult to apply to your particular situation, so let's go over your points.
问题1:这是假设是正确的或仍在执行的await关键字低于code
在code下面的等待异步调用完成时的关键字才会执行。在此期间,因为你的方法被标记为异步,控制将被直到你的方法完成返回到你的方法的调用者。
从上面的MSDN链接:
The code below the "await" keyword will only be executed when the async call completes. In the meantime, since your method is marked "async", control will be returned to the caller of your method until your method completes.From the MSDN link above:
Task<string> getStringTask = client.GetStringAsync("http://msdn.microsoft.com");
// You can do work here that doesn't rely on the string from GetStringAsync.
DoIndependentWork();
// The await operator suspends AccessTheWebAsync.
// - AccessTheWebAsync can't continue until getStringTask is complete.
// - Meanwhile, control returns to the caller of AccessTheWebAsync.
// - Control resumes here when getStringTask is complete.
// - The await operator then retrieves the string result from getStringTask.
string urlContents = await getStringTask;
我觉得评论是相当的解释。
I think the comments are quite explanatory.
其次假设我叫了服务方法和异步需要返回它的结果。 return语句是等待关键字的下面。
Secondly suppose I called a service method async and need to return its result. The return statement is below the await keyword.
问题2:什么时候是return语句命中,异步调用完成后或之前
在
问题3:我想使用该服务调用和异步操作的结果不会帮助,因为我想在已经返回的结果被打调用方法。我明白这可以通过使用Result属性,这使得呼叫SYNCHRONUS来完成。那么是什么在DB的操作使用异步的原因,他们是实际需要的80%的时间在大多数应用程序的人。
假设你需要做三个不相关的数据库查询来完成你的服务,然后执行基于结果的计算,然后完成。如果你这样做顺序,你不得不等待,直到每个操作完成。如果使用异步调用,那么C#将运行三个查询并行和你的服务可能完成更快。
Suppose you need to make three unrelated DB queries to complete your service, then perform a calculation based on the results, then finish. If you did this sequentially, you'd have to wait until each operation completes. If you use async calls, then C# will run the three queries in parallel and your service might finish much sooner.
还有,返回任务操作可以用作期货。请参见,几个模式如何基于并行工作讨论期货和合并的结果。
Also, operations that return Task can be used as Futures. See MSDN on Futures where several patterns are discussed on how to parallelize work based on futures and merge the results.
如果你的服务只需要一个DB调用那么它肯定会更糟为你异步调用它。
If your service only needs one DB call then it will definitely be worse for you to call it async.
问题4:我如何使用异步操作DB使用?是否有可能和recomended?
ADO.NET现在包括异步方法 ReadAsync和NextResultAsync 。
ADO.NET now includes async methods ReadAsync and NextResultAsync .
这是绝对有可能的,因为推荐的这种讨论更完整的比我可以写here.
It is definitely possible, as for recommended this discussion is much more complete than I could write here.
问题5:在这scenraio将异步操作是有益的,似乎每个API只是做异步操作现在没有理由吗?还是我错过使用异步OPS点?
异步操作是非常有用的轻松并行任何长期运行的操作,而不会在线程问题。如果你的方法只能做一件事情,或简单的(快速)事物的顺序,那么是的,它是没用的,去异步。不过,如果你有一个以上的长期运行的操作,这是更容易且不易出错通过异步比它是做管理线程并行他们。
async operations are very useful to easily parallelize any long running operations without running into threading problems. If your method only does one thing, or a sequence of simple (fast) things, then yes it is useless to go async. However if you have more than one long running operations, it is far easier and less error prone to parallelize them via async than it is to do it managing threads.
这篇关于在C#中如何等待异步工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!