C # is implemented with existing. NET event bridging simple code

Source: Internet
Author: User
This article mainly introduces the implementation of C # with the existing. NET event bridging a simple instance of the relevant data, the need for friends can refer to the following

C # is implemented with existing. NET event Bridging simple instances

Rx provides a factory method for with. NET, so that you can use the rich combination, filtering, and resource management capabilities provided by any type of data flow. This topic examines the Fromeventpattern operator, which allows the. NET events are "imported" into Rx as observable sequences. Each time an event is raised, the OnNext message is passed to the observable sequence. You can then process the event data just like any other observable sequence.

Rx is not intended to replace an existing asynchronous programming model, such as a. NET event, an asynchronous pattern, or a task Parallel library. However, when you try to write an event, the factory method of Rx will give you the convenience that you cannot find in the current programming model. This is especially true for resource maintenance (for example, when to unsubscribe) and filtering (for example, choosing what type of data to receive). In this topic and later in the topics, you can learn how these RX features help you with asynchronous programming.

Convert. NET events to Rx observable sequence

The following example creates a simple mouse move event. NET event handler and print the location of the mouse in the label of the Windows Form.

Using system.linq;using system.windows.forms;using system.reactive;using system.reactive.linq;using System;using Winform;using system.reactive.disposables;class program {  static void Main ()   {     var lbl = new Label ();      var frm = new Form {Controls = {lbl}};      frm. MouseMove + = (sender, args) =     {       lbl. Text = args. Location.tostring ();     };     Application.Run (frm);  }; }

To import an event into Rx, you can use the Fromeventpattern operator and provide a EventArgs object that will be raised by the event to be bridged. The Fromeventpattern operator is used to receive the object sender and some EventArgs events, and uses reflection to find these add/Remove methods for you. It then converts the given event to an observable sequence with the Eventpattern type, which captures the sender and event arguments.

For proxies with one parameter (non-standard event), you can use the fromevent operator, which requires a pair of functions for attaching and detaching handlers.

In the following example, we convert the mouse movement event stream of a Windows Form to an observable sequence. Subscribers receive a ONNEXT notification every time a mouse move event is triggered. We can then examine the EventArgs value of such notifications and get the position of the mouse movement.

Using system.linq;using system.windows.forms;using system.reactive;using system.reactive.linq;using System;using Winform;using system.reactive.disposables;class program {  static void Main ()   {     var lbl = new Label ();      var frm = new Form {Controls = {lbl}};      iobservable<eventpattern<mouseeventargs>> move = observable.fromeventpattern<mouseeventargs> ( frm, "MouseMove");     Move. Subscribe (evt = {                lbl. Text = evt. EventArgs.Location.ToString ();             }) ;     Application.Run (frm);  }; }

Note that in this example, move becomes an observable sequence that we can proceed further. A query that uses the LINQ operator to observe the sequence of topics will show you how to project this sequence into a collection of point types and filter its contents so that the application receives only values that meet certain criteria.

Cleanup of event handlers is the responsibility of the IDisposable object returned by the Subscribe method. Calling Dispose (done by reaching the end of Use-block in this example) frees all resources that are in use by the sequence that includes the underlying event handler. This essentially cancels the subscription activity on your behalf.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.