Amoeba.Windows.MainWindow.TimerThread C# (CSharp) Method

TimerThread() private method

private TimerThread ( ) : void
return void
        private void TimerThread()
        {
            try
            {
                var spaceCheckStopwatch = new Stopwatch();
                var backupStopwatch = new Stopwatch();
                var updateStopwatch = new Stopwatch();
                var uriUpdateStopwatch = new Stopwatch();
                var compactionStopwatch = new Stopwatch();
                var garbageCollectStopwatch = new Stopwatch();

                spaceCheckStopwatch.Start();
                backupStopwatch.Start();
                updateStopwatch.Start();
                uriUpdateStopwatch.Start();
                compactionStopwatch.Start();

                for (;;)
                {
                    Thread.Sleep(1000);
                    if (_closed) return;

                    {
                        if (_diskSpaceNotFoundException || _cacheSpaceNotFoundException)
                        {
                            this.Dispatcher.Invoke(DispatcherPriority.Background, new Action(() =>
                            {
                                _connectStopMenuItem_Click(null, null);
                                _convertStopMenuItem_Click(null, null);
                            }));
                        }

                        if (_connectionSettingManager.State == ManagerState.Stop
                            && (Settings.Instance.Global_IsConnectRunning && Settings.Instance.Global_ConnectionSetting_IsEnabled))
                        {
                            _connectionSettingManager.Start();
                        }
                        else if (_connectionSettingManager.State == ManagerState.Start
                            && (!Settings.Instance.Global_IsConnectRunning || !Settings.Instance.Global_ConnectionSetting_IsEnabled))
                        {
                            _connectionSettingManager.Stop();
                        }

                        if (_overlayNetworkManager.State == ManagerState.Stop
                            && (Settings.Instance.Global_IsConnectRunning && Settings.Instance.Global_I2p_SamBridge_IsEnabled))
                        {
                            _overlayNetworkManager.Start();
                        }
                        else if (_overlayNetworkManager.State == ManagerState.Start
                            && (!Settings.Instance.Global_IsConnectRunning || !Settings.Instance.Global_I2p_SamBridge_IsEnabled))
                        {
                            _overlayNetworkManager.Stop();
                        }

                        if (_amoebaManager.State == ManagerState.Stop
                            && Settings.Instance.Global_IsConnectRunning)
                        {
                            _amoebaManager.Start();

                            Log.Information("Start");
                        }
                        else if (_amoebaManager.State == ManagerState.Start
                            && !Settings.Instance.Global_IsConnectRunning)
                        {
                            _amoebaManager.Stop();

                            Log.Information("Stop");
                        }

                        if ((_amoebaManager.EncodeState == ManagerState.Stop && _amoebaManager.DecodeState == ManagerState.Stop)
                            && Settings.Instance.Global_IsConvertRunning)
                        {
                            _amoebaManager.EncodeStart();
                            _amoebaManager.DecodeStart();
                        }
                        else if ((_amoebaManager.EncodeState == ManagerState.Start && _amoebaManager.DecodeState == ManagerState.Start)
                            && !Settings.Instance.Global_IsConvertRunning)
                        {
                            _amoebaManager.EncodeStop();
                            _amoebaManager.DecodeStop();
                        }

                        if (_diskSpaceNotFoundException)
                        {
                            Log.Warning(LanguagesManager.Instance.MainWindow_DiskSpaceNotFound_Message);

                            this.Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() =>
                            {
                                MessageBox.Show(
                                    this,
                                    LanguagesManager.Instance.MainWindow_DiskSpaceNotFound_Message,
                                    "Warning",
                                    MessageBoxButton.OK,
                                    MessageBoxImage.Warning);
                            }));

                            _diskSpaceNotFoundException = false;
                        }

                        if (_cacheSpaceNotFoundException)
                        {
                            Log.Warning(LanguagesManager.Instance.MainWindow_CacheSpaceNotFound_Message);

                            this.Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() =>
                            {
                                MessageBox.Show(
                                    this,
                                    LanguagesManager.Instance.MainWindow_CacheSpaceNotFound_Message,
                                    "Warning",
                                    MessageBoxButton.OK,
                                    MessageBoxImage.Warning);
                            }));

                            _cacheSpaceNotFoundException = false;
                        }
                    }

                    if (Settings.Instance.Global_IsConnectRunning && spaceCheckStopwatch.Elapsed.TotalMinutes >= 1)
                    {
                        spaceCheckStopwatch.Restart();

                        try
                        {
                            var drive = new DriveInfo(Directory.GetCurrentDirectory());

                            if (drive.AvailableFreeSpace < NetworkConverter.FromSizeString("256MB"))
                            {
                                _diskSpaceNotFoundException = true;
                            }
                        }
                        catch (Exception e)
                        {
                            Log.Warning(e);
                        }

                        try
                        {
                            if (!string.IsNullOrWhiteSpace(_serviceManager.Config.Cache.Path))
                            {
                                var drive = new DriveInfo(Path.GetDirectoryName(Path.GetFullPath(_serviceManager.Config.Cache.Path)));

                                if (drive.AvailableFreeSpace < NetworkConverter.FromSizeString("256MB"))
                                {
                                    _diskSpaceNotFoundException = true;
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            Log.Warning(e);
                        }
                    }

                    if (backupStopwatch.Elapsed.TotalMinutes >= 30)
                    {
                        backupStopwatch.Restart();

                        try
                        {
                            _catharsisManager.Save(_configrationDirectoryPaths["CatharsisManager"]);
                            _transferLimitManager.Save(_configrationDirectoryPaths["TransfarLimitManager"]);
                            _overlayNetworkManager.Save(_configrationDirectoryPaths["OverlayNetworkManager"]);
                            _connectionSettingManager.Save(_configrationDirectoryPaths["ConnectionSettingManager"]);
                            _amoebaManager.Save(_configrationDirectoryPaths["AmoebaManager"]);
                            Settings.Instance.Save(_configrationDirectoryPaths["Settings"]);
                        }
                        catch (Exception e)
                        {
                            Log.Warning(e);
                        }
                    }

                    if (updateStopwatch.Elapsed.TotalDays >= 1)
                    {
                        updateStopwatch.Restart();

                        try
                        {
                            if (Settings.Instance.Global_Update_Option == UpdateOption.Check
                               || Settings.Instance.Global_Update_Option == UpdateOption.Update)
                            {
                                this.CheckUpdate(false);
                            }
                        }
                        catch (Exception e)
                        {
                            Log.Warning(e);
                        }
                    }

                    if (uriUpdateStopwatch.Elapsed.TotalHours >= 1)
                    {
                        uriUpdateStopwatch.Restart();

                        try
                        {
                            _connectionSettingManager.Update();
                        }
                        catch (Exception e)
                        {
                            Log.Warning(e);
                        }
                    }

                    if (!garbageCollectStopwatch.IsRunning || garbageCollectStopwatch.Elapsed.TotalMinutes >= 30)
                    {
                        garbageCollectStopwatch.Restart();

                        try
                        {
                            this.GarbageCollect();
                        }
                        catch (Exception e)
                        {
                            Log.Warning(e);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Log.Error(e);
            }
        }