5

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.

  • The page you link to contains a link ["How to implement..."](http://msdn.microsoft.com/en-us/library/e7a34yad.aspx), for more details you need to say what/how you need it. – H H Nov 18 '10 at 13:23

2 Answers2

5

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 which improves testability.

gcode
  • 2,954
  • 4
  • 21
  • 32
StanislawSwierc
  • 2,571
  • 17
  • 23
1

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.

Brandon Montgomery
  • 6,924
  • 3
  • 48
  • 71
  • The async operation will be doing some simple processing. The async operation will not be hammering any resources (db, filesystem etc). And it wont get called very often. The example you give uses the AsyncOperationManager class. There seems to be a lot of options on the table. What criteria do you use to decide your approach? – Junior Developer Nov 18 '10 at 14:00
  • I do the easiest thing possible (usually easiest to maintain) that will get the job done with the performance I need. Usually BackgroundWorker. – Brandon Montgomery Nov 18 '10 at 14:28