Exemple #1
0
        public void TrySetSystemClock()
        {
            if (_timeService.IsComputerTimeCorrect())
            {
                _logger.LogDebug("Time is correnct, no need to set it up once again");

                if (_applicationConfiguration.CountSystemRunningTime)
                {
                    Task.Run(() =>
                    {
                        _stopwatchService.StartTimer();
                        _stopwatchService.RunTimer();
                    });
                }

                if (_applicationConfiguration.CountNetworkActivity)
                {
                    _nicService.InitializeNICs();

                    Task.Run(() =>
                    {
                        _nicService.StartNicsMonitoring();
                    });
                }

                if (!_applicationConfiguration.UserActivityIntegration)
                {
                    ApplicationShutdown();
                }
                else
                {
                    _logger.LogDebug("Monitoring user activity started");
                }
            }

            else
            {
                _logger.LogDebug($"Selected date format: {_dateAndTimeFormat.DateFormat}, time format: {_dateAndTimeFormat.TimeFormat}");

                _timeService.SetSystemClock().GetAwaiter().GetResult();

                if (!_applicationConfiguration.UserActivityIntegration)
                {
                    ApplicationShutdown();
                }
                else
                {
                    _logger.LogDebug("Monitoring user activity started");
                }
            }
        }
        public async Task SetSystemClock()
        {
            _logger.LogDebug("Trying to set system time");

            string networkTime = await Task.Run(() => GetNetworkTime());

            if (networkTime == null)
            {
                throw new ArgumentNullException($"Incorrect time format: {networkTime}");
            }

            SYSTEMTIME systime = new SYSTEMTIME(Convert.ToDateTime(networkTime).ToUniversalTime());

            SetSystemTime(ref systime);

            if (_windowConfiguration.Beep)
            {
                for (int i = 0; i < _windowConfiguration.BeepCount; i++)
                {
                    Console.Beep(_windowConfiguration.SuccessBeepFrequency, _windowConfiguration.SuccessBeepDuration);

                    await Task.Delay(_windowConfiguration.DelayBetweenBeep);
                }
            }

            _logger.LogDebug($"System time set to: {networkTime}");

            if (_applicationConfiguration.CountSystemRunningTime)
            {
                await Task.Run(() =>
                {
                    _stopwatchService.StartTimer();
                    _stopwatchService.RunTimer();
                });
            }

            if (_applicationConfiguration.CountNetworkActivity)
            {
                await Task.Run(() =>
                {
                    _nicService.InitializeNICs();
                    _nicService.StartNicsMonitoring();
                });
            }
        }