csharp Rx样本

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp Rx样本相关的知识,希望对你有一定的参考价值。

        public void Start()
        {
            var player = new Alarm();
            //player.Play();
            LogManager.GetCurrentClassLogger().Info("starting to monitor.");

            var timedObserver = Observable.Interval(TimeSpan.FromSeconds(50));
            var netObserver = StatusObserverFactory.GetNetworkStatusObserver();
            var powObserver = StatusObserverFactory.GetPowerStatusObserver();

            timedObserver.CombineLatest(powObserver, netObserver, (i, pow, net) => !(pow || net))
                //.DistinctUntilChanged()
            .Subscribe(s => React(player, s),
                err => LogManager.GetCurrentClassLogger().Error("Error: {0}", err));
        }
    internal static class StatusObserverFactory
    {
        static public IObservable<bool> GetPowerStatusObserver()
        {
            var obs = Observable.FromEvent<PowerModeChangedEventHandler, PowerModeChangedEventArgs>(
                evc => (sender, evt) => evc(evt),
                h => SystemEvents.PowerModeChanged += h,
                h => SystemEvents.PowerModeChanged -= h
            ).Where(evt => evt.Mode == PowerModes.StatusChange)
            .Select(
              _ => SystemInformation.PowerStatus.PowerLineStatus == PowerLineStatus.Online
            )
            .StartWith(true);
            obs.Subscribe(e => LogManager.GetCurrentClassLogger().Trace("power : {0}", e));

            LogManager.GetCurrentClassLogger().Trace("Subscribe to power event");
            return obs;
        }

        static public IObservable<bool> GetNetworkStatusObserver()
        {
            var obs = Observable.FromEvent<NetworkAvailabilityChangedEventHandler, NetworkAvailabilityEventArgs>(
                evc => (sender, evt) => evc(evt),
                h => NetworkChange.NetworkAvailabilityChanged += h,
                h => NetworkChange.NetworkAvailabilityChanged -= h
            )
            .Select(evt => evt.IsAvailable)
            .StartWith(true);
            obs.Subscribe(e => LogManager.GetCurrentClassLogger().Trace("network : {0}", e));

            LogManager.GetCurrentClassLogger().Trace("Subscribe to network event");
            return obs;
        }
    }

以上是关于csharp Rx样本的主要内容,如果未能解决你的问题,请参考以下文章

csharp 使用Akka.net对事件进行分区(linqpad - 参考akka.net和Rx-main)

csharp 使用Akka.net对事件进行分区(linqpad - 参考akka.net和Rx-main)

csharp 多线程样本

csharp 样本转换

csharp 样本复制数据

csharp 自适应卡片动作样本