Timer should be disabled and events detached if no longer needed.

So IDisposable is introduced which handles the above...
This commit is contained in:
Jürgen Holzer 2016-07-22 21:38:58 +02:00
parent 234a522f8f
commit 08137421ea

View File

@ -8,7 +8,7 @@ using System.Timers;
namespace SpotifyAPI.Local namespace SpotifyAPI.Local
{ {
public class SpotifyLocalAPI public class SpotifyLocalAPI : IDisposable
{ {
[DllImport("user32.dll")] [DllImport("user32.dll")]
private static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo); 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 const int KeyeventfKeyup = 0x2;
private readonly RemoteHandler _rh; private readonly RemoteHandler _rh;
private readonly Timer _eventTimer; private Timer _eventTimer;
private StatusResponse _eventStatusResponse; private StatusResponse _eventStatusResponse;
public event EventHandler<TrackChangeEventArgs> OnTrackChange; public event EventHandler<TrackChangeEventArgs> OnTrackChange;
@ -64,9 +64,21 @@ namespace SpotifyAPI.Local
{ {
_rh = new RemoteHandler(); _rh = new RemoteHandler();
AttachTimer(50);
}
public SpotifyLocalAPI(int timerIntervall)
{
_rh = new RemoteHandler();
AttachTimer(timerIntervall);
}
private void AttachTimer(int intervall)
{
_eventTimer = new Timer _eventTimer = new Timer
{ {
Interval = 50, Interval = intervall,
AutoReset = false, AutoReset = false,
Enabled = false Enabled = false
}; };
@ -333,5 +345,13 @@ namespace SpotifyAPI.Local
Process.Start(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"spotify\spotifywebhelper.exe")); 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;
}
} }
} }