Rx.Net multiple Mergerd Observables & Throttle() not working as expected

折月煮酒 提交于 2020-01-06 07:04:54

问题


I have multiple source Obserables that emit Unit.Default instances merged into one stream and the later being .Throttle()'ed to yield a result after 300ms of 'silence'

var columnVisibilityChangedObservable =
    Observable.FromEventPattern<ColumnVisibilityChangedEventArgs>(
        handler => this.ColumnVisibilityChanged += handler,
        handler => this.ColumnVisibilityChanged -= handler)
    .Select(_ => Unit.Default);

var dataBindingCompleteObservable =
    Observable.FromEventPattern<GridViewBindingCompleteEventHandler, GridViewBindingCompleteEventArgs>(
        handler => this.DataBindingComplete += handler,
        handler => this.DataBindingComplete -= handler)
    .Select(_ => Unit.Default);

var childViewExpandedObservable =
    Observable.FromEventPattern<ChildViewExpandedEventHandler, ChildViewExpandedEventArgs>(
        handler => this.ChildViewExpanded += handler,
        handler => this.ChildViewExpanded -= handler)
    .Select(_ => Unit.Default);

var filterChangedObservable =
    Observable.FromEventPattern<GridViewCollectionChangedEventHandler, GridViewCollectionChangedEventArgs>(
        handler => this.FilterChanged += handler,
        handler => this.FilterChanged -= handler)
    .Select(_ => Unit.Default);

var groupByChangedObservable =
    Observable.FromEventPattern<GridViewCollectionChangedEventHandler, GridViewCollectionChangedEventArgs>(
        handler => this.GroupByChanged += handler,
        handler => this.GroupByChanged -= handler)
    .Select(_ => Unit.Default);

var groupExpandedChangedObservable =
    Observable.FromEventPattern<GroupExpandedEventHandler, GroupExpandedEventArgs>(
        handler => this.GroupExpanded += handler,
        handler => this.GroupExpanded -= handler)
    .Select(_ => Unit.Default);

var columnWidthChangedObservable =
    Observable.FromEventPattern<ColumnWidthChangedEventHandler, ColumnWidthChangedEventArgs>(
        handler => this.ColumnWidthChanged += handler,
        handler => this.ColumnWidthChanged -= handler)
    .Select(_ => Unit.Default);

// merge all source streams into one
var mergedStream = Observable
    .Merge(
        columnVisibilityChangedObservable,
        dataBindingCompleteObservable,
        childViewExpandedObservable,
        filterChangedObservable,
        groupByChangedObservable,
        groupExpandedChangedObservable,
        columnWidthChangedObservable)
    .Do(_ =>
    {
        Debug.WriteLine("{0:HH:mm:ss fff}: Event!", DateTime.Now);
    });

// take that merged stream and throttle it by a dueTime of 300ms
return mergedStream
    .Throttle(TimeSpan.FromMilliseconds(300)) // use throttling approach to reduce event rate
    .Do(_ =>
        {
            Debug.WriteLine("{0:HH:mm:ss fff}: Update!", DateTime.Now);
        }
    }).Subscribe();

However, my problem is that this way it does not work as expected - I would expect, as the .Throttle() documentation states, that there shouldn't be any "Update!" events within 300ms of each other.. but there are (this is a real example output):

11:28:39 432: Event!
11:28:39 479: Event!
11:28:39 534: Event!
11:28:39 536: Event!
11:28:39 577: Event!
11:28:39 635: Event!
11:28:39 655: Event!
11:28:39 706: Event!
11:28:39 711: Event!
11:28:39 774: Event!
11:28:39 775: Event!
11:28:39 777: Event!
11:28:39 892: Update!
11:28:39 938: Update!
11:28:39 974: Update!
11:28:40 014: Update!
11:28:40 077: Update!
11:28:40 429: Event!
11:28:40 435: Event!
11:28:40 485: Event!
11:28:40 486: Event!
11:28:40 733: Update!
11:28:40 743: Event!
11:28:40 744: Event!
11:28:40 745: Event!
11:28:40 749: Update!
11:28:40 778: Event!
11:28:40 795: Update!
11:28:41 044: Update!

Those multiple "Update!"s shortly & within 300ms after one other are what's strange / look wrong to me - I'd expect there to be less overall and none within 300ms of each other.

So I am pretty sure I am doing something wrong - but I am not sure why. Any ideas what's going on here / where the problem is?

Update:

Thing's I've tried that didn't change this odd behaviour

  • Increasing the Throttle() dueTime to something way longer (3000ms) - no change
  • Called ThreadPool.SetMinThreads(100,100) (see James' comment below)
  • replace the .Do() call(s) with normal OnNext actions

What's maybe also worth noting is that this code runs inside a Visual Studio (2013) extension and the main / UI thread is quite busy when this happens. Moreover, the default scheduler appears to be a dispatcher one (VS and all).

来源:https://stackoverflow.com/questions/46361358/rx-net-multiple-mergerd-observables-throttle-not-working-as-expected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!