コード例 #1
0
        private static void RunMineRestartMonitor(EventAggregator bus)
        {
            object sync = new object();

            var disksToMine = new DriveInfo[0];

            void OnCompletedPlotFile(CompletedPlotFileNotification e)
            {
                lock (sync)
                {
                    disksToMine = disksToMine.Union(new[] { e.Disk }).ToArray();
                }

                _logger.LogDebug($"mining disks: {disksToMine.Select(d => d.Name).Aggregate((disks, next) => $"{disks}, {next}")}");

                bus.GetEvent <PubSubEvent <RestartMiningNotification> >().Publish(new RestartMiningNotification(disksToMine));
            }

            void OnMiningUnavailableForDrive(MiningUnavailableForDriveNotification e)
            {
                var restartMining = false;

                lock (sync)
                {
                    if (disksToMine.Contains(e.Disk))
                    {
                        restartMining = true;
                        disksToMine   = disksToMine.Except(new[] { e.Disk }).ToArray();
                    }
                }

                if (restartMining)
                {
                    bus.GetEvent <PubSubEvent <RestartMiningNotification> >().Publish(new RestartMiningNotification(disksToMine));
                }
            }

            bus.GetEvent <PubSubEvent <CompletedPlotFileNotification> >().Subscribe(OnCompletedPlotFile, true);
            bus.GetEvent <PubSubEvent <MiningUnavailableForDriveNotification> >().Subscribe(OnMiningUnavailableForDrive, true);
        }