Rx.Net多个Mergerd Observables& Throttle()无法按预期工作

时间:2017-09-22 09:29:57

标签: c# system.reactive reactive-programming rx.net

我有多个源Obserables,它们将Unit.Default实例合并为一个流,然后是.Throttle(),以便在300毫秒的“沉默”之后产生结果

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();

然而,我的问题是,这种方式不能按预期工作 - 我希望,正如.Throttle()文档所述,不应该有任何更新!&#34;相互之间300毫秒内的事件..但有(这是一个真实的例子输出):

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!

那些多次&#34;更新!&#34; s短暂&amp;在一个人之后的300ms之内,对我来说有什么奇怪/看错了 - 我预计总体上会有更少的错误,而且相互之间的距离不会超过300毫秒。

所以我很确定我做错了什么 - 但我不确定为什么。任何想法在这里/问题出在哪里?

更新

我尝试过没有改变这种奇怪的行为

  • 将Throttle()dueTime增加到更长的时间(3000ms) - 无变化
  • 名为ThreadPool.SetMinThreads(100,100)(见詹姆斯&#39;评论如下)
  • 使用正常的OnNext操作替换.Do()调用

可能还值得注意的是,此代码在Visual Studio(2013)扩展中运行,并且当发生这种情况时,主/ UI线程非常繁忙。此外,默认调度程序似乎是一个调度程序(VS和所有)。

0 个答案:

没有答案