Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to replace traditional event handling in C# with the new Reactive framework?

All examples on System.Reactive.dll I've seen so far deal with Events, EventArgs and EventHandlers, I wonder whether someone can show me an example where event notification is handled without this.

For instance, in Microsoft's XNA framework, you have a static method called Mouse.GetState() which will return the current MouseState (with mouseState.LeftButton == ButtonState.Pressed you could see whether the left button is pressed, for instance). So there are no EventArgs, Events etc. in the first place and I think this could serve as an example of achieving event notification without introducing the concept of an event at all.

Could System.Reactive be of help here? Can anyone wrap this into an example with System.Reactive?

like image 598
kitsune Avatar asked Aug 23 '09 18:08

kitsune


People also ask

What is event handling in C?

C event handlers allow you to interface more easily to external systems, but allowing you to provide the glue logic. The POS includes a small open source C compiler (TCC) which will dynamically compile and link your C code at runtime. Alternatively, you can precompile and ship a DLL/EXE with your functions.

What are event handler methods?

Event handlers In the event handler, you perform the actions that are required when the event is raised, such as collecting user input after the user clicks a button. To receive notifications when the event occurs, your event handler method must subscribe to the event.

How you can add an event handler?

Right-click the control for which you want to handle the notification event. On the shortcut menu, choose Add Event Handler to display the Event Handler Wizard. Select the event in the Message type box to add to the class selected in the Class list box.


2 Answers

Quote from an article that I found helpful:

In C#, a programmer uses reactive programming whenever he specifies a call-back for an asynchronous operation or a handler for an event. When the asynchronous operation finishes or when the handled event takes place, a method gets called and executed as a reaction to the event.

Mouse.GetState() is a method that gets the current state of the Mouse. Reactive programming would be useful if you were trying to continuously handle (react to) changes to the state of the mouse.

For example, you could create a wrapper that continuously polls the mouse state, and then notifies its observers (essentially back to publishing events here). You could then write code to handle (react to) those updates.

like image 154
Nader Shirazie Avatar answered Oct 13 '22 21:10

Nader Shirazie


Polling mouse or keyboard states works in a game where you have direct access to update methods that are the heartbeat of the application. The update method IS your event that is called and passed on to many objects, not much different to MouseDown. So even here you do rely on the observer pattern.

like image 30
galaktor Avatar answered Oct 13 '22 21:10

galaktor