本文整理汇总了C#中BehaviorSubject.CombineLatest方法的典型用法代码示例。如果您正苦于以下问题:C# BehaviorSubject.CombineLatest方法的具体用法?C# BehaviorSubject.CombineLatest怎么用?C# BehaviorSubject.CombineLatest使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BehaviorSubject
的用法示例。
在下文中一共展示了BehaviorSubject.CombineLatest方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AsyncObservableThreadsWithBetterThrottleOnComputeAndIsCalculating
public void AsyncObservableThreadsWithBetterThrottleOnComputeAndIsCalculating()
{
Console.WriteLine("Starting Thread " + Thread.CurrentThread.ManagedThreadId);
BehaviorSubject<int> s1 = new BehaviorSubject<int>(2);
BehaviorSubject<int> s2 = new BehaviorSubject<int>(3);
BehaviorSubject<int> sum = new BehaviorSubject<int>(5);
List<int> computeThreads = new List<int>();
List<int> receiveThreads = new List<int>();
IScheduler throttleScheduler = new EventLoopScheduler();
Func<IScheduler> getComputeScheduler = () => new EventLoopScheduler();
IScheduler receiveScheduler = new EventLoopScheduler();
IObservable<Tuple<int, int>> sumObservable = s1.CombineLatest(s2, Tuple.Create).Throttle(TimeSpan.FromMilliseconds(100), throttleScheduler);
IDisposable sumObservableSubscription = null;
using (sumObservable.Subscribe(v =>
{
if (sumObservableSubscription != null)
{
Console.WriteLine("Canceling previous.");
sumObservableSubscription.Dispose();
}
sumObservableSubscription = Observable.Create<int>((o, token) => Task.Factory.StartNew(() =>
{
Thread.Sleep(200);
if (!token.IsCancellationRequested)
{
Console.WriteLine("Computing value " + v.Item1 + " + " + v.Item2 + " = " + (v.Item1 + v.Item2) + " on Thread " + Thread.CurrentThread.ManagedThreadId + ".");
computeThreads.Add(Thread.CurrentThread.ManagedThreadId);
o.OnNext(v.Item1 + v.Item2);
}
o.OnCompleted();
return Disposable.Empty;
})).ObserveOn(receiveScheduler).Subscribe(v2 =>
{
Console.WriteLine("Received value " + v2 + " on Thread " + Thread.CurrentThread.ManagedThreadId + ".");
receiveThreads.Add(Thread.CurrentThread.ManagedThreadId);
});
}))
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
Thread.Sleep(150);
s2.OnNext(1);
Thread.Sleep(50);
s1.OnNext(4);
Thread.Sleep(250);
s2.OnNext(4);
Thread.Sleep(150);
s1.OnNext(1);
Thread.Sleep(350);
stopwatch.Stop();
Console.WriteLine("Total Time: " + stopwatch.ElapsedMilliseconds + " ms");
foreach (KeyValuePair<int, int> p in
computeThreads.GroupBy(v => v).Select(g => new KeyValuePair<int, int>(g.Key, g.Count())))
{
Console.WriteLine(p.Value + " computes on Thread " + p.Key);
}
foreach (KeyValuePair<int, int> p in
receiveThreads.GroupBy(v => v).Select(g => new KeyValuePair<int, int>(g.Key, g.Count())))
{
Console.WriteLine(p.Value + " receives on Thread " + p.Key);
}
}
}
示例2: RxOnlyAsyncObservableThreadsWithBetterThrottleOnComputeAndIsCalculating
public void RxOnlyAsyncObservableThreadsWithBetterThrottleOnComputeAndIsCalculating()
{
Console.WriteLine("Starting Thread " + Thread.CurrentThread.ManagedThreadId);
BehaviorSubject<int> s1 = new BehaviorSubject<int>(2);
BehaviorSubject<int> s2 = new BehaviorSubject<int>(3);
List<int> computeThreads = new List<int>();
List<int> receiveThreads = new List<int>();
IScheduler throttleScheduler = new EventLoopScheduler();
IScheduler computeScheduler = NewThreadScheduler.Default;
IScheduler receiveScheduler = new EventLoopScheduler();
IObservable<Tuple<int, int>> sumObservable = s1.CombineLatest(s2, Tuple.Create).Throttle(TimeSpan.FromMilliseconds(100), throttleScheduler);
Func<CalculatedPropertyHelper, int, int, Task<int>> calculate = async (helper, v1, v2) =>
{
Thread.Sleep(200);
helper.CheckCancellationToken();
Console.WriteLine("Computing value " + v1 + " + " + v2 + " = " + (v1 + v2) + " on Thread " + Thread.CurrentThread.ManagedThreadId + ".");
computeThreads.Add(Thread.CurrentThread.ManagedThreadId);
return await Task.FromResult(v1 + v2);
};
BehaviorSubject<int> sum = new BehaviorSubject<int>(0);
IDisposable scheduledTask = computeScheduler.ScheduleAsync(async (scheduler, token) =>
{
await scheduler.Yield();
sum.OnNext(await calculate(new CalculatedPropertyHelper(scheduler, token), s1.Value, s2.Value));
});
using (sumObservable.Subscribe(v =>
{
if (scheduledTask != null)
{
Console.WriteLine("Canceling previous.");
scheduledTask.Dispose();
}
scheduledTask = computeScheduler.ScheduleAsync(async (scheduler, token) =>
{
await scheduler.Yield();
sum.OnNext(await calculate(new CalculatedPropertyHelper(scheduler, token), v.Item1, v.Item2));
});
}))
{
using (sum.ObserveOn(receiveScheduler).Subscribe(v2 =>
{
Console.WriteLine("Received value " + v2 + " on Thread " + Thread.CurrentThread.ManagedThreadId + ".");
receiveThreads.Add(Thread.CurrentThread.ManagedThreadId);
}))
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
Thread.Sleep(150);
s2.OnNext(1);
Thread.Sleep(50);
s1.OnNext(4);
Thread.Sleep(250);
s2.OnNext(4);
Thread.Sleep(150);
s1.OnNext(1);
Thread.Sleep(350);
stopwatch.Stop();
Console.WriteLine("Total Time: " + stopwatch.ElapsedMilliseconds + " ms");
foreach (KeyValuePair<int, int> p in
computeThreads.GroupBy(v => v).Select(g => new KeyValuePair<int, int>(g.Key, g.Count())))
{
Console.WriteLine(p.Value + " computes on Thread " + p.Key);
}
foreach (KeyValuePair<int, int> p in
receiveThreads.GroupBy(v => v).Select(g => new KeyValuePair<int, int>(g.Key, g.Count())))
{
Console.WriteLine(p.Value + " receives on Thread " + p.Key);
}
}
}
}