简体   繁体   中英

How would I go about implementing the event-based asynchronous pattern?

Hi I want to implement the event-based asynchronous pattern using c#. The Microsoft documentation below is really helpful but quite rightly it leaves out the implementation details.

http://msdn.microsoft.com/en-us/library/ms228969.aspx

What are my options for implementing this pattern? Should I use the Delegate.BeginInvoke, ThreadPool.QueueUserWorkItem, new Thread(), BackgroundWorker class, or something else?

I'm creating a simple code library. It should be usable in a Windows form, console app, or ASP.NET context.

It would be great to see some sample code.

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 or BackgroundWorkers. AsyncFunc handles everything for you. Moreover, this approach allows you to separate your business logic from pattern specific implementation what improves testability.

http://asyncfunc.codeplex.com

You can find comparison of MSDN recommended way and AsyncFunc implementation of the same component:

MSDN vs. AsyncFunc

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM