當前位置: 首頁>>代碼示例>>C#>>正文


C# BehaviorSubject.Select方法代碼示例

本文整理匯總了C#中BehaviorSubject.Select方法的典型用法代碼示例。如果您正苦於以下問題:C# BehaviorSubject.Select方法的具體用法?C# BehaviorSubject.Select怎麽用?C# BehaviorSubject.Select使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在BehaviorSubject的用法示例。


在下文中一共展示了BehaviorSubject.Select方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: ThreadSwitching

        public void ThreadSwitching()
        {
            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 switchScheduler = new EventLoopScheduler();
            IScheduler computeScheduler = new EventLoopScheduler();

            IObservable<int> sumObservable = s1.Select(v => new Tuple<int, int>(v, s2.Value)).Merge(s2.Select(v => new Tuple<int, int>(s1.Value, v))).Throttle(TimeSpan.FromMilliseconds(100), switchScheduler).Select(v =>
                {
                    Console.WriteLine("Not yet switching computing value " + v.Item1 + " + " + v.Item2 + " = " + (v.Item1 + v.Item2) + " from Thread " + Thread.CurrentThread.ManagedThreadId + ".");
                    return v;
                }).Select(v =>
                    {
                        Console.WriteLine("Switching computing value " + v.Item1 + " + " + v.Item2 + " = " + (v.Item1 + v.Item2) + " from Thread " + Thread.CurrentThread.ManagedThreadId + ".");
                        return v;
                    }).ObserveOn(computeScheduler).Select(v =>
                        {
                            Console.WriteLine("Already switched computing value " + v.Item1 + " + " + v.Item2 + " = " + (v.Item1 + v.Item2) + " to Thread " + Thread.CurrentThread.ManagedThreadId + ".");
                            return v;
                        }).Select(v =>
                            {
                                Console.WriteLine("Computing value " + v.Item1 + " + " + v.Item2 + " = " + (v.Item1 + v.Item2) + " on Thread " + Thread.CurrentThread.ManagedThreadId + ".");
                                computeThreads.Add(Thread.CurrentThread.ManagedThreadId);
                                return v.Item1 + v.Item2;
                            });

            sumObservable.Subscribe(sum.OnNext);
            sum.ObserveOn(new EventLoopScheduler()).Subscribe(v =>
                {
                    Console.WriteLine("Received value " + v + " on Thread " + Thread.CurrentThread.ManagedThreadId + ".");
                    receiveThreads.Add(Thread.CurrentThread.ManagedThreadId);
                });
            Thread.Sleep(150);
            s2.OnNext(1);
            Thread.Sleep(50);
            s1.OnNext(4);
            Thread.Sleep(150);
            s2.OnNext(4);
            Thread.Sleep(250);
            s1.OnNext(1);
            Thread.Sleep(150);
            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);
            }
        }
開發者ID:jam40jeff,項目名稱:RxMvvm,代碼行數:56,代碼來源:Sandbox.cs


注:本文中的BehaviorSubject.Select方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。