关于套接字编程的APM,EAP和TPL [英] APM, EAP and TPL on Socket Programming

查看:58
本文介绍了关于套接字编程的APM,EAP和TPL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现 [...]异步与Begin [...] .net异步API之间的区别问题,但

I found Difference between […]Async and Begin[…] .net asynchronous APIs question but this answer confused me a little bit.

谈到这些模式,斯蒂芬说:

Talking about these patterns, Stephen said:

大多数*异步方法(带有相应的* Completed事件)都使用基于事件的异步模式.较旧的(但仍然非常有效)Begin *和End *是一种称为异步编程模型"的模式.

Most *Async methods (with corresponding *Completed events) are using the Event-Based Asynchronous Pattern. The older (but still perfectly valid) Begin* and End* is a pattern called the Asynchronous Programming Model.

Socket类是该规则的一个例外.它的* Async方法没有任何对应的事件;从本质上说,这只是APM所做的,可以避免过多的内存分配.

The Socket class is an exception to this rule; its *Async methods do not have any corresponding events; it's essentially just APM done in a way to avoid excessive memory allocations.

我知道,因为使用*异步方法更有效,至少在套接字方面.但随后他提到了任务并行库:

I get it as using *Async methods are more efficient, at least when it comes to sockets. But then he mentioned Task Parallel Library:

但是,基于任务并行库的APM和EBAP都被一种更加灵活的方法所取代.由于TPL可以轻松包装APM,因此较旧的类可能不会直接更新.扩展方法用于为旧的APM方法提供等效的任务.

However, both APM and EBAP are being replaced with a much more flexible approach based on the Task Parallel Library. Since the TPL can wrap APMs easily, older classes will likely not be updated directly; extension methods are used to provide Task equivalents for the old APM methods.

我在MSDN上找到了 TPL和传统.NET异步编程,我知道TPL的基础知识,创建任务,取消,继续等,但是我仍然不明白这些:

I found TPL and Traditional .NET Asynchronous Programming on MSDN, I know the basics of TPL, creating tasks, cancellations, continuations, etc but I still fail to understand these:

相比,异步编程模型(APM)和基于事件的异步模式(EAP)有什么优势? TPL如何轻松包装APM ,意味着APM和EAP都被 TPL取代了?

What are the advantages of Asynchronous Programming Model (APM) and Event-based Asynchronous Pattern (EAP) compared to each other? How does TPL can wrap APMs easily mean that both APM and EAP are being replaced with TPL?

最重要的是:我应该在套接字编程中使用哪个;

And most importantly: Which should I use in socket programming;

  • APM?
  • EAP?
  • 由任务包装的APM或EAP?
  • 在任务中使用Socket类的阻塞方法来实现TPL吗?
  • 其他?

推荐答案

TPL如何轻松包装APM,这意味着APM和EAP都被TPL取代了?

How does TPL can wrap APMs easily mean that both APM and EAP are being replaced with TPL?

不是.是否将APM和EAP替换为TAP(任务异步模式),或者在新的API中与此无关.出于各种原因,我希望TAP取代APM和EAP.对我来说,主要原因是您为使用TAP编写的代码组成更好.进行 .ContinueWith(/* ... */).ContinueWith(/* ... */)的读取效果通常比编写通过Begin/来链接异步调用所需的相应代码要好得多.结束方法,即使您不考虑选项,也可以将其传递给ContinueWith来确定是否应继续执行.TPL还为Task提供了各种组合器,例如 WaitAll WaitAny ,这些组合器可以使某些情况变得更加容易.通过async/await关键字在C#和VB.NET中提供的语言支持将使其变得更加容易.

It doesn't. Wether APM and EAP will be replaced by TAP (Task Asynchronous Pattern) or not in new APIs has nothing to do with this. I would expect TAP to replace APM and EAP for a variety of reasons. The main reason to me is that the code you write for using the TAP composes much better. Doing .ContinueWith(/* ... */).ContinueWith(/* ... */) generally reads much better than the corresponding code you would need to write to chain async calls through Begin/End methods, even if you don't take into account the options you can pass to ContinueWith to determine if the continuation should run. The TPL also provides various combinators for Tasks, such as WaitAll and WaitAny, that can make some scenarios much easier. The language support coming in C# and VB.NET via the async/await keywords will make this even easier.

能够在TAP中包装APM使得切换到此模式变得更加容易,因为这意味着您不必重写现有代码即可使其适应新模型.

Being able to wrap APMs in the TAP makes it easier to switch to this pattern because it means you don't have to rewrite existing code to make it fit in the new model.

我应该在套接字编程中使用哪个?

Which should I use in socket programming?

我建议使用TAP封装Socket上的APM方法.除非您能证明将Begin/End方法包装到Task中的额外开销是可伸缩性/足够快与否之间的区别,否则我将利用TAP编码的简便性.

I would recommend using the TAP wrapping the APM methods on Socket. Unless you can prove that the extra overhead of wrapping the Begin/End methods into a Task are the difference between scalable/fast enough or not, I would take advantage the ease of coding of the TAP.

这篇关于关于套接字编程的APM,EAP和TPL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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