public MediaPlayerHandle(CoreDispatcher dispatcher) { BackgroundSettings.RemoveForegroundId(); _dispatcher = dispatcher; _notifier = new BackgroundNotifier(_id); _subscriptionHandle = new BackgroundSubscriptionHandle(OnMessageReceivedFromBackground); _notificationQueue = new ValueSetWorkerQueue(HandleNotification); BackgroundSettings.SetForegroundId(_id); }
public BackgroundAudioRun(Guid id) { _id = id; _foregroundNotifier = new ForegroundNotifier(id); _timer = new Timer(_ => { var metadataHandler = _metadataHandler; if (null == metadataHandler) { return; } metadataHandler.Refresh(); }, null, Timeout.InfiniteTimeSpan, Timeout.InfiniteTimeSpan); _notificationQueue = new ValueSetWorkerQueue(vs => { HandleNotification(vs); return(TplTaskExtensions.CompletedTask); }); _watchdogTimer = new Timer( _ => { Debug.WriteLine("BackgroundAudioRun watchdog"); var barks = 1; try { var foregroundId = BackgroundSettings.ForegroundId; if (!foregroundId.HasValue || _completionSource.Task.IsCompleted) { Interlocked.Exchange(ref _watchdogBarks, 0); StopWatchdog(); return; } barks = Interlocked.Increment(ref _watchdogBarks); if (barks > 3) { Debug.WriteLine("BackgroundAudioRun watchdog exiting"); _completionSource.TrySetCanceled(); Cancel(); return; } _foregroundNotifier.Notify(BackgroundNotificationType.Ping); } catch (Exception ex) { Debug.WriteLine("BackgroundAudioRun watchdog failed: " + ex.ExtendedMessage()); } RequestWatchdog(TimeSpan.FromTicks(WatchdogTimeout.Ticks >> barks)); }, null, Timeout.InfiniteTimeSpan, Timeout.InfiniteTimeSpan); }
public BackgroundAudioRun(Guid id) { _id = id; _foregroundNotifier = new ForegroundNotifier(id); _timer = new Timer(_ => { var metadataHandler = _metadataHandler; if (null == metadataHandler) return; metadataHandler.Refresh(); }, null, Timeout.InfiniteTimeSpan, Timeout.InfiniteTimeSpan); _notificationQueue = new ValueSetWorkerQueue(vs => { HandleNotification(vs); return TplTaskExtensions.CompletedTask; }); _watchdogTimer = new Timer( _ => { Debug.WriteLine("BackgroundAudioRun watchdog"); var barks = 1; try { var foregroundId = BackgroundSettings.ForegroundId; if (!foregroundId.HasValue || _completionSource.Task.IsCompleted) { Interlocked.Exchange(ref _watchdogBarks, 0); StopWatchdog(); return; } barks = Interlocked.Increment(ref _watchdogBarks); if (barks > 3) { Debug.WriteLine("BackgroundAudioRun watchdog exiting"); _completionSource.TrySetCanceled(); Cancel(); return; } _foregroundNotifier.Notify(BackgroundNotificationType.Ping); } catch (Exception ex) { Debug.WriteLine("BackgroundAudioRun watchdog failed: " + ex.ExtendedMessage()); } RequestWatchdog(TimeSpan.FromTicks(WatchdogTimeout.Ticks >> barks)); }, null, Timeout.InfiniteTimeSpan, Timeout.InfiniteTimeSpan); }