From ec8b7a3d3dad53161e9268322abd918c5403ae54 Mon Sep 17 00:00:00 2001 From: Jonas Dellinger Date: Fri, 9 Sep 2016 14:56:37 +0200 Subject: [PATCH] Made some LocalAPI methods async - Updated example, now without SynchronizingObject --- SpotifyAPI.Example/LocalControl.cs | 37 ++++++++++++++++++++++------- SpotifyAPI/Local/SpotifyLocalAPI.cs | 17 ++++++------- 2 files changed, 38 insertions(+), 16 deletions(-) diff --git a/SpotifyAPI.Example/LocalControl.cs b/SpotifyAPI.Example/LocalControl.cs index 1d65d5a8..e393dffe 100644 --- a/SpotifyAPI.Example/LocalControl.cs +++ b/SpotifyAPI.Example/LocalControl.cs @@ -4,6 +4,7 @@ using SpotifyAPI.Local.Models; using System; using System.Diagnostics; using System.Globalization; +using System.Threading.Tasks; using System.Windows.Forms; namespace SpotifyAPI.Example @@ -22,7 +23,7 @@ namespace SpotifyAPI.Example _spotify.OnTrackChange += _spotify_OnTrackChange; _spotify.OnTrackTimeChange += _spotify_OnTrackTimeChange; _spotify.OnVolumeChange += _spotify_OnVolumeChange; - _spotify.SynchronizingObject = this; + //_spotify.SynchronizingObject = this; artistLinkLabel.Click += (sender, args) => Process.Start(artistLinkLabel.Tag.ToString()); albumLinkLabel.Click += (sender, args) => Process.Start(albumLinkLabel.Tag.ToString()); @@ -108,22 +109,42 @@ namespace SpotifyAPI.Example private void _spotify_OnVolumeChange(object sender, VolumeChangeEventArgs e) { + if (InvokeRequired) + { + Invoke(new Action(() => _spotify_OnVolumeChange(sender, e))); + return; + } volumeLabel.Text = (e.NewVolume * 100).ToString(CultureInfo.InvariantCulture); } private void _spotify_OnTrackTimeChange(object sender, TrackTimeChangeEventArgs e) { - timeLabel.Text = $"{FormatTime(e.TrackTime)}/{FormatTime(_currentTrack.Length)}"; + if (InvokeRequired) + { + Invoke(new Action(() => _spotify_OnTrackTimeChange(sender, e))); + return; + } + timeLabel.Text = $@"{FormatTime(e.TrackTime)}/{FormatTime(_currentTrack.Length)}"; timeProgressBar.Value = (int)e.TrackTime; } private void _spotify_OnTrackChange(object sender, TrackChangeEventArgs e) { + if (InvokeRequired) + { + Invoke(new Action(() => _spotify_OnTrackChange(sender, e))); + return; + } UpdateTrack(e.NewTrack); } private void _spotify_OnPlayStateChange(object sender, PlayStateEventArgs e) { + if (InvokeRequired) + { + Invoke(new Action(() => _spotify_OnPlayStateChange(sender, e))); + return; + } UpdatePlayingStatus(e.Playing); } @@ -132,19 +153,19 @@ namespace SpotifyAPI.Example Connect(); } - private void playUrlBtn_Click(object sender, EventArgs e) + private async void playUrlBtn_Click(object sender, EventArgs e) { - _spotify.PlayURL(playTextBox.Text, contextTextBox.Text); + await _spotify.PlayURL(playTextBox.Text, contextTextBox.Text); } - private void playBtn_Click(object sender, EventArgs e) + private async void playBtn_Click(object sender, EventArgs e) { - _spotify.Play(); + await _spotify.Play(); } - private void pauseBtn_Click(object sender, EventArgs e) + private async void pauseBtn_Click(object sender, EventArgs e) { - _spotify.Pause(); + await _spotify.Pause(); } private void prevBtn_Click(object sender, EventArgs e) diff --git a/SpotifyAPI/Local/SpotifyLocalAPI.cs b/SpotifyAPI/Local/SpotifyLocalAPI.cs index 35d1789d..62a7fead 100644 --- a/SpotifyAPI/Local/SpotifyLocalAPI.cs +++ b/SpotifyAPI/Local/SpotifyLocalAPI.cs @@ -4,6 +4,7 @@ using System.ComponentModel; using System.Diagnostics; using System.IO; using System.Runtime.InteropServices; +using System.Threading.Tasks; using System.Timers; namespace SpotifyAPI.Local @@ -234,17 +235,17 @@ namespace SpotifyAPI.Local /// /// Pause function /// - public void Pause() + public async Task Pause() { - _rh.SendPauseRequest(); + await _rh.SendPauseRequest(); } /// /// Play function /// - public void Play() + public async Task Play() { - _rh.SendPlayRequest(); + await _rh.SendPlayRequest(); } /// @@ -265,9 +266,9 @@ namespace SpotifyAPI.Local /// /// Contexts are basically a queue in spotify. a song can be played within a context, meaning that hitting next / previous would lead to another song. Contexts are leveraged by widgets as described in the "Multiple tracks player" section of the following documentation page: https://developer.spotify.com/technologies/widgets/spotify-play-button/ /// - public void PlayURL(string uri, string context = "") + public async Task PlayURL(string uri, string context = "") { - _rh.SendPlayRequest(uri, context); + await _rh.SendPlayRequest(uri, context); } /// @@ -275,9 +276,9 @@ namespace SpotifyAPI.Local /// /// The Spotify URI [Obsolete("This method doesn't work with the current spotify version.")] - public void AddToQueue(string uri) + public async Task AddToQueue(string uri) { - _rh.SendQueueRequest(uri); + await _rh.SendQueueRequest(uri); } ///