How do I subscribe to, handle, and keep track of multiple events in C#

Viewed 80

So I am able to subscribe to a single event and perform functions on that one event as follows:

socket.eventPool.Subscribe("flight", 
  new EventHandler<flightArgs>((x, y) => 
    Console.WriteLine(y.STR1.temp + " " + y.STR1.alt)));
socket.SubscribeEquip("gulfstream");

However, I want to be able to generalize the idea and subscribe to and keep track of multiple different events. For example, in addition to "gulfstream" I want to be able to do this on "boeing747" and other known stuff for which I want to perform the same or different operations. However, the problem is I do not want C# to get confused in the act of subscribing to 2 events or more as the socket.eventPool.Subscribe("flight", new EventHandler((x, y) => Console.WriteLine(y.STR1.temp + " " + y.STR1.alt))); would remain the same.

Is there any advice on how to do this? The syntax for the event stuff is a little weird so I am not sure how this would be implemented in classes, for example. Would classes be the way to go and instantiate objects that subscribe to and use the events separately? Examples would be very welcome.

1 Answers

I would personally use reactivex for such event handling. It makes it easier to handle subscriptions.

e.g.

var parallel = Observable.WhenAll(
    ObservableWWW.Get("http://google.com/"),
    ObservableWWW.Get("http://bing.com/"),
    ObservableWWW.Get("http://unity3d.com/")
);

parallel.Subscribe(xs =>
{
    Debug.Log(xs[0].Substring(0, 100)); // google
    Debug.Log(xs[1].Substring(0, 100)); // bing
    Debug.Log(xs[2].Substring(0, 100)); // unity
});
Related