且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在C#中如何等待异步工作

更新时间:2022-05-28 09:25:48

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

Question # 1: Is this assumption correct or the code below the await keyword is still executed?

在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语句命中,异步调用完成后或之前

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.

假设你需要做三个不相关的数据库查询来完成你的服务,然后执行基于结果的计算,然后完成。如果你这样做顺序,你不得不等待,直到每个操作完成。如果使用异步调用,那么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.

还有,返回任务操作可以用作期货。请参见 MSDN期货,几个模式如何基于并行工作讨论期货和合并的结果。

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?

Question # 4: How can I use async with DB operations? Is it possible and 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点?

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?

异步操作是非常有用的轻松并行任何长期运行的操作,而不会在线程问题。如果你的方法只能做一件事情,或简单的(快速)事物的顺序,那么是的,它是没用的,去异步。不过,如果你有一个以上的长期运行的操作,这是更容易且不易出错通过异步比它是做管理线程并行他们。

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.