C# implements simple instances of bridging with existing .NET events
Rx provides factory methods to bridge with existing asynchronous sources in .NET so that you can use the rich combination, filtering and resource management capabilities provided by any type of data stream. This topic checks for the FromEventPattern operator, which allows .NET events to be "imported" into Rx as observable sequences. Each time an event is raised, the OnNext message will be passed to the observable sequence. You can then process event data like any other observable sequence.
Rx is not intended to replace existing asynchronous programming models such as .NET events, asynchronous patterns, or task parallel libraries. However, when you try to write events, Rx's factory approach will give you convenience that is not found in the current programming model. This is especially true for resource maintenance (e.g., when to unsubscribe) and filtering (e.g., choosing what type of data to receive). In this and later topics, you can learn how these Rx features can help you with asynchronous programming.
Convert .NET events to Rx observable sequence
The following example creates a simple .NET event handler for mouse movement events and prints the location of the mouse in the label of a Windows form.
using ; using ; using ; using ; using System; using WinForm; using ; class Program { static void Main() { var lbl = new Label(); var frm = new Form { Controls = { lbl } }; += (sender, args) => { = (); }; (frm); }; }
To import events into Rx, you can use the FromEventPattern operator and provide the EventArgs object that will be raised by the event to be bridged. The FromEventPattern operator is used to receive events from the object sender and some EventArgs and to find these add/remove methods for you using reflection. It then converts the given event into an observable sequence with type EventPattern, which captures the sender and event parameters.
For proxy with one parameter (non-standard event), you can use the FromEvent operator, which requires a pair of functions for appending and separating the handler.
In the following example, we convert the mouse movement event stream of Windows forms into observable sequences. Subscribers will receive OnNext notifications each time a mouse movement event is triggered. We can then check the EventArgs value of such notifications and get the position where the mouse moves.
using ; using ; using ; using ; using System; using WinForm; using ; class Program { static void Main() { var lbl = new Label(); var frm = new Form { Controls = { lbl } }; IObservable<EventPattern<MouseEventArgs>> move = <MouseEventArgs>(frm, "MouseMove"); (evt => { = (); }) ; (frm); }; }
Note that in this example, move becomes an observable sequence that we can operate further. Query observable sequence topics using the LINQ operator will show you how to project this sequence into a collection of point types and filter its contents so that the application only receives values that meet certain conditions.
The cleanup of the event handler is the responsibility of the IDisposable object returned by the Subscribe method. Calling Dispose (completed by reaching the end of the use-block in this example) will free up all resources that are being used by the sequence including the underlying event handler. This essentially means unsubscribe to your event.
Thank you for reading, I hope it can help you. Thank you for your support for this site!