在.NET异步方法(!)澄清? [英] Asynchronous methods(!) clarification in .net?

查看:116
本文介绍了在.NET异步方法(!)澄清?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读了很多最近关于这个话题,仍然我需要澄清一下

异步方法的是发经济的整体思路的:

允许许多任务的几个线程运行。这是通过使用硬件驱动程序来完成这项工作,同时释放线程回到线程池,以便完成它可以服务器的其他工作。

Allow many tasks to run on a few threads. this is done by using the hardware driver to do the job while releasing the thread back to the thread-pool so it can server other jobs.

请注意。

我的不可以谈论的异步委托的这关系另一个线程(并行执行任务的来电的)。

I'm not talking about asynchronous delegates which ties another thread (execute a task in parallel with the caller).

不过,我已经看到了2个主要类型的异步方法的例子:

However I've seen 2 main types of asynchronous methods examples :

  • code样品(来自书籍)谁只是使用的现有的I / O异步操作的的BeginXXX / endXX 如: Stream.BeginRead
    我找不到任何的异步方法的样本,的的使用的现有的.NET的I / O操作如: Stream.BeginRead

  • Code samples (from books) who only uses existing I/O asynchronous operations as beginXXX / endXX e.g. Stream.BeginRead.
    And I couldn't find any asynchronous methods samples which don't use existing .net I/O operations e.g. Stream.BeginRead )

code如 <样本STRONG>这 (和<一href="http://webcache.googleusercontent.com/search?q=cache%3atooJMko50V0J%3awww.$c$cproject.com/Articles/42734/Using-IHttpAsyncHandler-and-XMLHtt$p$pquest-to-push%20&cd=3&hl=en&ct=clnk&gl=il"相对=nofollow>这个)。这犯规实际调用异步操作(虽然笔者的认为他! - 但事实上他却会导致一个线程的的)

Code samples like this (and this). which doesnt actually invoking an asynchronous operation (although the author thinks he is - but he actually causes a thread to block !)

问:

确实异步方法的仅用于与.NET的I / O如的BeginXXX,EndXXX

Does asynchronous methods are used only with .net I/O existing methods like BeginXXX , EndXXX ?

我的意思是,如果我想创建自己的异步方法的如 BeginMyDelay(INT MS,...){..},EndMyDelay(...)。我不能做它没有扎阻塞线程它....正确的?

I mean , If I want to create my own asynchronous methods like BeginMyDelay(int ms,...){..} , EndMyDelay(...). I couldn't done it without tie a blocked thread to it....correct?

非常感谢你。

P.S。请注意这个问题被标记为.NET 4中,而不是.net4.5

推荐答案

您正在谈论的 APM 。 APM广泛使用的操作系统的概念,被称为的 IO完成端口的。这就是为什么不同的IO操作使用APM的最佳人选。

You're talking about APM. APM widely uses OS concept, known as IO Completion ports. That's why different IO operations are the best candidates to use APM.

您可以编写自己的APM的方法。 但是,事实上,这些方法将要么在现有的APM方法,否则将IO密集,并且会使用一些本机操作系统机制(如 FilesStream ,它采用重叠文件IO)。

You could write your own APM methods. But, in fact, these methods will be either over existing APM methods, or they will be IO-bound, and will use some native OS mechanism (like FilesStream, which uses overlapped file IO).

有关计算密集型异步操作APM不仅会增加复杂性,海事组织。

For compute-bound asynchronous operations APM only will increase complexity, IMO.

这一点更多的澄清。

与硬件的工作是异步其性质。硬件需要一个时间来执行的请求 - newtork卡必须发送或接收数据,硬盘必须读/写等,如果IO是同步的,线程,这是产生IO请求,正在等待回应。在这里,APM帮助 - 你不应该等待,只要执行别的东西,当IO就完成了,我会打电话给你,APM说

Work with hardware is asynchronous by its nature. Hardware needs a time to perform request - newtork card must send or receive data, HDD must read/write etc. If IO is synchronous, thread, which was generated IO request, is waiting for response. And here APM helps - you shouldn't wait, just execute something else, and when IO will be complete, I'll call you, says APM.

主要的一点 - CPU的运算执行外

The main point - operation is performing outside of CPU.

当你写的任何计算密集型操作,它使用的CPU为它执行没有任何IO,没有什么在这里等。因此,APM coludn't帮助 - 如果你需要的CPU,你需要线程 - 你需要的线程池

When you're writing any compute-bound operation, which will use CPU for it execution without any IO, there's nothing to wait here. So, APM coludn't help - if you need CPU, you need thread - you need thread pool.

这篇关于在.NET异步方法(!)澄清?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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