TaskCompletionSource - 试图了解螺纹异步工作 [英] TaskCompletionSource - Trying to understand threadless async work
问题描述
我想了解TaskCompletionSource及其关系的目的,异步/螺纹工作。我想我有一般的想法,但我想确保我的理解是正确的。
I'm trying to understand the purpose of TaskCompletionSource and its relation to async/threadless work. I think I have the general idea but I want to make sure my understanding is correct.
我第一次开始寻找到任务并行Librry(TPL),以找出是否有一个很好的方式来创建的自己螺纹/异步工作(说你想提高你的ASP.NET网站的可扩展性),加上理解TPL看起来将在未来很重要的(异步
/ 等待
)。害得我到TaskCompletionSource。
I first started looking into the Task Parallel Librry (TPL) to figure out if there was a good way to create your own threadless/async work (say your trying to improve scalability of your ASP.NET site) plus understanding of the TPL looks like it will be very important in the future (async
/await
). Which led me to the TaskCompletionSource.
从我的理解,它看起来像加TaskCompletionSource到你的类并没有真正做多少作为使你的编码异步的之一;如果你仍然在执行同步code,然后调用您的code将阻止。我认为,这是微软的API甚至如此。例如说,在'DownloadStringTaskAsync关闭WebClient类的,任何设置/同步code它们最初做将阻止该code你的执行必须在某些线程上运行,无论是当前线程或你将不得不分拆一个新的。
From my understanding it looks like adding TaskCompletionSource to a one of your classes doesn't really do much in as making your coding async; if you're still executing sync code then the call to your code will block. I think this is even true of microsoft apis. For example say in 'DownloadStringTaskAsync" off of WebClient class, any setup / sync code they are doing initially will block. The code your executing has to run on some thread, either the current thread or you will have to spin off a new one.
所以你用TaskCompletionSource在自己的code,当你调用微软其他异步调用所以你的类的客户端不具有创建你的类不会阻止一个新的线程。
So you use the TaskCompletionSource in your own code when your calling other async calls from microsoft so the client of your classes doesn't have to create a new thread for your class to not block.
不知道微软如何做自己的API异步内部。比如有一个新的异步方法关闭SqlDataReader中对于.NET 4.5。我知道有IO完成端口。我认为这是一个低层次的抽象(C ++)指出可能是最C#开发人员不会使用。不知道IO完成端口将为比如数据库或网络调用运行(HTTP),或者如果它只是用于文件IO。
Not sure how microsoft does their async apis internally. For example there is a new Async methods off of the SqlDataReader for .Net 4.5. I know there is IO Completion Ports. I think it's a lower level abstraction (C++?) that probably most C# developers won't use. Not sure if IO completion Ports will work for say Database or network calls (HTTP) or if its just used for file IO.
所以,问题是我在我的理解是否正确是否正确?是否有某些事情我已经重新$ P $错误psented?
So the question is am I correct in my understanding correct? Are there certain things I've represented incorrectly?
推荐答案
TaskCompletionSource
用于创建任务
对象不执行code。
TaskCompletionSource
is used to create Task
objects that don't execute code.
他们用相当多的由微软新的API异步 - 随时有I / O型异步操作(或其他非基于CPU异步操作,如超时)。此外,你写的异步任务
方法将使用TCS完成其返回工作
。
They're used quite a bit by Microsoft's new async APIs - any time there's I/O-based asynchronous operations (or other non-CPU-based asynchronous operations, like a timeout). Also, any async Task
method you write will use TCS to complete its returned Task
.
我有一个博客帖子创建任务,讨论不同的方法来创建工作
实例。它是从写的异步
/ 等待
角度(不是TPL角度),但它仍然适用于这里。
I have a blog post Creating Tasks that discusses different ways to create Task
instances. It's written from an async
/await
perspective (not a TPL perspective), but it still applies here.
另见斯蒂芬Toub出色的帖子:
Also see Stephen Toub's excellent posts:
- TaskCompletionSource的本质
- 为创建任务机制
- 的await什么; (使用
TaskCompletionSource
到等待
的任何东西)。 - 使用任务落实APM模式(创建
开始
/结束
TaskCompletionSource
)。
- The Nature of TaskCompletionSource
- Mechanisms for Creating Tasks
- await anything; (using
TaskCompletionSource
toawait
anything). - Using Tasks to implement the APM Pattern (creating
Begin
/End
usingTaskCompletionSource
).
这篇关于TaskCompletionSource - 试图了解螺纹异步工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!