5

I am looking at implementing an API where multiple operations can run concurrently. The Event-based Asynchronous Pattern has been recommended by MS:

// Asynchronous methods.
public void Method1Async(string param, object userState);
public event Method1CompletedEventHandler Method1Completed;

public void CancelAsync(object userState);
public bool IsBusy { get; }

However this seems a little clumsy to be - it requires clients to filter out replies which are not meant for them and disconnect the event handler when done etc. What I thinking is something more like:

AsyncOperation1(string parm, Action<T> callback)

Each client gets get's its own results delivered directly. I am having trouble figuring out how to support cancelation elegantly. I guess the obvious thing is for AsyncOperation1 to return some kind of token which can be passed into a CancelAsync method. I'd like to find out more about what other async patterns are in common usage in .Net or in other languages that can be translated appropriately

Shane
  • 2,271
  • 3
  • 27
  • 55

1 Answers1

4

Take a look at Reactive Extensions, you can return an Observable which can be subscribed to by the clients.

The subscription returns an object that implements IDisposable which is the mechanism to cancel the subscription, just dispose of the object.

For example:

IObservable<T> o = Method1Async(params);

var observer = o.Subscribe(r => {/*do stuff with the result*/},
                           ex => {/* do stuff with the exception */);

// decide to cancel
observer.Dispose();

You can install the Reactive Extensions bits using nuget, just "install-package rx-main"

Matt
  • 2,984
  • 1
  • 24
  • 31
  • I think you a describing a way of adapting and existing Event-based Asynchronous API to an IObservable. How would one go about implementing a new API from scratch? – Shane Dec 07 '11 at 05:12
  • No, you can use Rx to implement your own async operations and provide implementations of IObservable back to your clients for them to subscribe to. Take a look at http://msdn.microsoft.com/en-us/library/hh211669(v=VS.103).aspx for info on the ISubject interface – Matt Dec 07 '11 at 05:16
  • However there are many, many existing methods for generating events which you can use to build the observables at a higher level than implementing the interfaces – Matt Dec 07 '11 at 05:19
  • Thanks - will take a look. Do you know if the paralell framework is also worth looking into? – Shane Dec 07 '11 at 05:27
  • Yeah TPL is worth looking at, this answer (http://stackoverflow.com/questions/4105206/will-reactive-extensions-rx-supercede-the-task-parallel-library) details some of the differences between the two – Matt Dec 07 '11 at 05:42
  • I played around a bit with Rx and it has some nice features but I am not convinced it is great match for my use case. Rx seems to be designed arround ansynchronous data streams but all I need is simple operations which may return single payloads. Additionaly, the IOvberaveable interface only supports a single operation with a generic name so you would need to have an object per operation. – Shane Dec 08 '11 at 03:44
  • It is based around events, but doesn't have to be about streams of them, it's used quite effectively to respond to single fire events, Observable.FromEventPattern for example. Not sure what you mean about the IObservable single operation with a generic name... it's more about an object that your clients can subscribe to, to perform what they want to do with the result when your async operation completes, also what to do if it throws an exception – Matt Dec 08 '11 at 09:16