Andyz Smith
Andyz Smith

Reputation: 708

using reactive 4.2.0 can I Scan for Count inside Subscribe

Can I access the parent collection inside subscribe to do a running Scan for Count? Shall I just run a submillisecond event handler to hog up my WPF message pump, WIFI interrupts and everything else to multi-threadedly keep updating the collection count when nobody is even using the mouse?

var MouseXCentiPixels =
Observable.FromEventPattern<MouseButtonEventArgs>(
target: this, 
eventName: nameof(System.Windows.Window.MouseDown))
.Select(_evt => _evt.EventArgs.GetPosition(relativeTo: this).X * 1.0E2)


MouseXCentiPixels
.Throttle(dueTime: TimeSpan.FromMilliseconds(6E1))
.Subscribe(onNext: (_MX) =>
{
    Task<Int32> GetCountQuickly = Task.Run<Int32>(async () =>
    {
    Int32 result1 = await MouseXCentiPixels.Scan(
    seed: 0, 
    accumulator: (CurrentAccumulation, CurrentItem) => 
        {
            var T = CurrentAccumulation; var P = CurrentItem;
            return 1;
        }
    }
    Int32 MultiThreadedQuickCount = GetCountQuickly.Wait(timeout: TimeSpan.FromMilliseconds(1E2));
}

);

Upvotes: 0

Views: 62

Answers (1)

Andyz Smith
Andyz Smith

Reputation: 708

Ok, I guess reactive is busy hogging everything up whenever you Scan anyway so here goes:

TimeSpan SampleTimeSpan = TimeSpan.FromSeconds(.50E0);

MouseXCentiPixels
.Select(_mousex => -1)
.Scan((accum, curr) => ++accum)
.Sample(interval: SampleTimeSpan)
.Buffer(count: 2, skip: 1)
.Select(bufGroupOfTwo => bufGroupOfTwo.Skip(1).FirstOrDefault() - bufGroupOfTwo.FirstOrDefault() )
.Subscribe(AccumulatedClickChangeDuringInterval =>
{
    Double ClicksPerSecond = AccumulatedClickChangeDuringInterval /    SampleTimeSpan.TotalSeconds;
    Double SecondsPerMinute = 60;
    Double ClicksPerMinute = ClicksPerSecond * SecondsPerMinute;

    Debug.WriteLine($" {nameof(ClicksPerMinute)} : {ClicksPerMinute:N1}");
});

Upvotes: 1

Related Questions