private async void ConsumePlaylist(object sender, DoWorkEventArgs e) { bool didCommitPause = false; while (true) { if (playerWorker.CancellationPending) { e.Cancel = true; break; } else if (isPaused) { if (didCommitPause == false) { if (currentlyPlaying.IsPlayableOffline) { await foobarService.Pause(); } else if (currentlyPlaying.IsPlayableOnline) { await spotifyService.PausePlayback(); } didCommitPause = true; } Thread.Sleep(500); continue; } else { if (tracks.Count > 0) { if (currentlyPlaying != null) { if (currentlyPlaying.IsPlayableOffline) { await foobarService.Pause(); } else if (currentlyPlaying.IsPlayableOnline) { await spotifyService.PausePlayback(); } } didCommitPause = false; currentlyPlaying = tracks.First(); tracks.Remove(currentlyPlaying); if (currentlyPlaying.IsPlayableOffline) { switch (currentlyPlaying.OfflinePlayer) { case SupportedMediaPlayers.Foobar2000: // TODO: maybe not this way string playlistPath = AppDomain.CurrentDomain.BaseDirectory + "_playzone_mix.m3u"; System.IO.StreamWriter sw = new System.IO.StreamWriter(playlistPath); sw.WriteLine(currentlyPlaying.FilePath); sw.Flush(); sw.Close(); sw.Dispose(); System.Diagnostics.Process.Start(playlistPath); break; } int sleepTime = currentlyPlaying.PlaytimeInSeconds * 1000 + 2000; if (sleepTime < 12000) { sleepTime = 12000; } Thread.Sleep(sleepTime); // + 2 sec for API delay // TODO: maybe not this way } else if (currentlyPlaying.IsPlayableOnline) { switch (currentlyPlaying.OnlinePlayer) { case SupportedMediaPlayers.Spotify: await spotifyService.ChangePlaybackMusic(currentlyPlaying.SpotifyURI); break; } int sleepTime = currentlyPlaying.PlaytimeInSeconds * 1000 + 2000; if (sleepTime < 12000) { sleepTime = 12000; } Thread.Sleep(sleepTime); // + 2 sec for API delay // TODO: maybe not this way } } } } }