guneysus
7/3/2017 - 8:03 AM

Rx Examples [.NET]

Rx Examples [.NET]

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Reactive;
using System.Reactive.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RxForms
{
    public partial class MainForm : Form
    {
        public MainForm ( )
        {
            InitializeComponent ( );

            IObservable<EventPattern<MouseEventArgs>> moveFrm = Observable.FromEventPattern<MouseEventArgs> (this, "MouseMove");
            IObservable<EventPattern<MouseEventArgs>> moveListbox = Observable.FromEventPattern<MouseEventArgs> (this.lstBox1, "MouseMove");

            IObservable<Point> frmPoints = from evt in moveFrm select evt.EventArgs.Location;
            IObservable<Point> lstPoints = from evt in moveListbox select evt.EventArgs.Location;


            
            var merged = frmPoints.Merge (lstPoints);

            var firstQuarter = from pos in merged
                               where pos.X >= this.Width / 2 && pos.Y <= this.Height / 2
                               select pos;

            var secondQuarter = from pos in merged
                                where pos.X >= this.Width / 2 && pos.Y >= this.Height / 2
                                select pos;


            var thirdQuarter = from pos in merged
                               where pos.X <= this.Width / 2 && pos.Y >= this.Height / 2
                               select pos;


            var fourthQuarter = from pos in merged
                                where pos.X <= this.Width / 2 && pos.Y <= this.Height / 2
                                select pos;

            firstQuarter.Subscribe ((Point pos) => Trace.WriteLine ($"{DateTime.Now.ToLongTimeString ( )} [1st]"));
            secondQuarter.Subscribe ((Point pos) => Trace.WriteLine ($"{DateTime.Now.ToLongTimeString ( )} [2nd]"));
            thirdQuarter.Subscribe ((Point pos) => Trace.WriteLine ($"{DateTime.Now.ToLongTimeString ( )} [3rd]"));
            fourthQuarter.Subscribe ((Point pos) => Trace.WriteLine ($"{DateTime.Now.ToLongTimeString ( )} [4th]"));


            merged.Buffer (TimeSpan.FromSeconds (1)).Subscribe ((IList<Point> positions) =>
            {
                positions.Average (_ => _.X);
            });

        }
    }
}