Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 有条件地组合两个Rx流_C#_Stream_Task Parallel Library_System.reactive_Reactive Programming - Fatal编程技术网

C# 有条件地组合两个Rx流

C# 有条件地组合两个Rx流,c#,stream,task-parallel-library,system.reactive,reactive-programming,C#,Stream,Task Parallel Library,System.reactive,Reactive Programming,我正在尝试使用Rx实现一个场景,其中我有两个热观测值。流1和流2。基于流1的数据,我需要启动流2或停止流2。然后使用CombineTest将两个流数据合并为一个。下面是我能想出的代码 有没有更好的方法来实现这一点 我如何使它更通用,就像我将有流1,然后流2。。n表示来自2.的每个流。。n有条件2。。n,它利用流1的数据检查其他流是否需要启动,然后以CombineTest方式组合所有数据 代码: IDisposable TfsDisposable = null;

我正在尝试使用Rx实现一个场景,其中我有两个热观测值。流1和流2。基于流1的数据,我需要启动流2或停止流2。然后使用CombineTest将两个流数据合并为一个。下面是我能想出的代码

  • 有没有更好的方法来实现这一点

  • 我如何使它更通用,就像我将有流1,然后流2。。n表示来自2.的每个流。。n有条件2。。n,它利用流1的数据检查其他流是否需要启动,然后以CombineTest方式组合所有数据

  • 代码:

            IDisposable TfsDisposable = null;
    
            // stream 1
            var hotObs = Observable.Timer(TimeSpan.Zero, TimeSpan.FromSeconds(1));
    
    
            // stream 2
            var hotObs2 = Observable.Timer(TimeSpan.Zero, TimeSpan.FromSeconds(1)).Publish();
    
    
            var observerHot =  hotObs.Do(a => 
            {
                // Based on Condition to start the second stream
                if (ConditionToStartStream2)
                {
                    TfsDisposable = TfsDisposable ?? hotObs2.Connect();
                }
            })
            .Do(a => 
            {
                // Based on condition 2 stop the second stream
                if (ConditionToStopStream2)
                {
                    TfsDisposable?.Dispose();
                    TfsDisposable = null;
                }
            }).Publish();
    
    
            // Merge both the stream using Combine Latest
            var finalMergedData  = hotObs.CombineLatest(hotObs2, (a, b) => { return string.Format("{0}, {1}", a, b); });
    
            // Display the result
            finalMergedData.Subscribe(a => { Console.WriteLine("result: {0}", a);  });
    
            // Start the first hot observable
            observerHot.Connect();
    
    玩一玩这个:

    var hotObs = Observable.Timer(TimeSpan.Zero, TimeSpan.FromSeconds(1.0));
    var hotObs2 = Observable.Timer(TimeSpan.Zero, TimeSpan.FromSeconds(0.3));
    
    var query =
        hotObs2.Publish(h2s =>
            hotObs.Publish(hs =>
                hs
                    .Select(a => a % 7 == 0 ? h2s : Observable.Empty<long>())
                    .Switch()
                    .Merge(hs)));
    

    嗨,我尝试使用上面提到的表达式,但是在订阅Observable时,我没有得到任何输出流数据。你能进一步解释一下吗?@BalrajSingh-我得到了一个输出,但它的行为不像我期望的那样-我有点不对劲。我认为这将接近你所需要的。我将在稍后对此进行研究,并提供一个解释。我确实看到了启动第二个流的条件。但没有条件结束它。即使结果不是必需的,第二条流还会继续运行吗?@BalrajSingh-对不起,忘了ping。我只是在浏览答案并玩弄它。将条件更改为%7==0我看到的问题是它没有打印流1的某些值。 0 1 2 3 1 2 3 4 5 6 7 23 24 25 8