///<summary> /// Gets another task which that the given task <paramref name="self"/> can be awaited with a <paramref name="timeout"/>. ///</summary> ///<param name="self">The task to be awaited.</param> ///<param name="timeout">The number of milliseconds to wait.</param> ///<returns> ///<c>true</c> if the <see cref="Task"/> completed execution within the allotted time; otherwise, <c>false</c>. ///</returns> publicstaticasync Task<bool> GetTaskWithTimeout(this Task self, int timeout)
///<summary> /// Stops the service. ///</summary> publicasync Task StopAsync() { _cancellationTokenSource.Cancel(); await _checkingStatusTask.GetTaskWithTimeout(StopTaskTimeout); if (!_checkingStatusTask.IsCompleted) { _logger.Warning($"Failed to stop checking status task within {StopTaskTimeout} ms - stopping anyway."); } }
privateasync Task RunCheckBatteryStatusPeriodicTask() { try { while (!_cancellationTokenSource.IsCancellationRequested) { var status = xxx(); if (status && !_isBatteryLowShown) { _isBatteryLowShown = true; _logger.Error("Battery is low."); } await Task.Delay(ElapsedMilliseconds, _cancellationTokenSource.Token); } } catch (Exception e) { _logger.ErrorEx(message: $"{nameof(_checkingStatusTask)} exception.", sourceType: nameof(BatteryMonitoringService), ex: e); }