我将如何实现基于事件的异步模式?
您好,我想使用 C# 实现基于事件的异步模式。下面的 Microsoft 文档确实很有帮助,但它忽略了实现细节,这是正确的。
http://msdn.microsoft.com/en-us/library/ms228969.aspx
实现此模式有哪些选择?我应该使用 Delegate.BeginInvoke、ThreadPool.QueueUserWorkItem、new Thread()、BackgroundWorker 类还是其他类?
我正在创建一个简单的代码库。它应该可以在 Windows 窗体、控制台应用程序或 ASP.NET 上下文中使用。
很高兴看到一些示例代码。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想实现基于事件的异步模式,则 AsyncFunc 是最简单的方法。
它极大地简化了这种模式。您无需担心
ThreadPools
、Threads
或BackgroundWorkers
。 AsyncFunc 会为您处理一切。此外,这种方法允许您将业务逻辑与特定于模式的实现分开,从而提高可测试性。If you want to implement Event-Based Asynchronous Pattern then AsyncFunc is the easiest way to go.
It simplifies this patter dramatically. You don't need to bother about
ThreadPools
,Threads
orBackgroundWorkers
. AsyncFunc handles everything for you. Moreover, this approach allows you to separate your business logic from pattern specific implementation which improves testability.阅读该文章的这一部分:
http://msdn.microsoft.com/en- us/library/9hk12d4y.aspx
话虽这么说,我建议您应该使用的技术取决于您的应用程序,因此没有更多关于这些异步操作将执行的操作以及调用它们的频率和频率的详细信息它们被称为什么情况,我无法就您的情况给出最佳使用技术的建议。
Read this section of that article:
http://msdn.microsoft.com/en-us/library/9hk12d4y.aspx
That being said, I would suggest that the technique you should use depends on your application, so without more details about what these async operations will be doing and how often they will be called and in what circumstances they are called, I can't give a recommendation on what the best technique to use is in your situation.