diff --git a/SpotifyAPI/Local/SpotifyLocalAPI.cs b/SpotifyAPI/Local/SpotifyLocalAPI.cs index 701bc8c7..2d639657 100644 --- a/SpotifyAPI/Local/SpotifyLocalAPI.cs +++ b/SpotifyAPI/Local/SpotifyLocalAPI.cs @@ -8,7 +8,7 @@ using System.Timers; namespace SpotifyAPI.Local { - public class SpotifyLocalAPI + public class SpotifyLocalAPI : IDisposable { [DllImport("user32.dll")] private static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo); @@ -49,7 +49,7 @@ namespace SpotifyAPI.Local private const int KeyeventfKeyup = 0x2; private readonly RemoteHandler _rh; - private readonly Timer _eventTimer; + private Timer _eventTimer; private StatusResponse _eventStatusResponse; public event EventHandler OnTrackChange; @@ -64,9 +64,21 @@ namespace SpotifyAPI.Local { _rh = new RemoteHandler(); + AttachTimer(50); + } + + public SpotifyLocalAPI(int timerIntervall) + { + _rh = new RemoteHandler(); + + AttachTimer(timerIntervall); + } + + private void AttachTimer(int intervall) + { _eventTimer = new Timer { - Interval = 50, + Interval = intervall, AutoReset = false, Enabled = false }; @@ -333,5 +345,13 @@ namespace SpotifyAPI.Local Process.Start(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"spotify\spotifywebhelper.exe")); } } + + public void Dispose() + { + if (_eventTimer == null) + return; + _eventTimer.Enabled = false; + _eventTimer.Elapsed -= ElapsedTick; + } } } \ No newline at end of file