Skip to content

Commit 0813742

Browse files
committed
Timer should be disabled and events detached if no longer needed.
So IDisposable is introduced which handles the above...
1 parent 234a522 commit 0813742

1 file changed

Lines changed: 23 additions & 3 deletions

File tree

‎SpotifyAPI/Local/SpotifyLocalAPI.cs‎

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
namespace SpotifyAPI.Local
1010
{
11-
public class SpotifyLocalAPI
11+
public class SpotifyLocalAPI : IDisposable
1212
{
1313
[DllImport("user32.dll")]
1414
private static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo);
@@ -49,7 +49,7 @@ public ISynchronizeInvoke SynchronizingObject
4949
private const int KeyeventfKeyup = 0x2;
5050

5151
private readonly RemoteHandler _rh;
52-
private readonly Timer _eventTimer;
52+
private Timer _eventTimer;
5353
private StatusResponse _eventStatusResponse;
5454

5555
public event EventHandler<TrackChangeEventArgs> OnTrackChange;
@@ -64,9 +64,21 @@ public SpotifyLocalAPI()
6464
{
6565
_rh = new RemoteHandler();
6666

67+
AttachTimer(50);
68+
}
69+
70+
public SpotifyLocalAPI(int timerIntervall)
71+
{
72+
_rh = new RemoteHandler();
73+
74+
AttachTimer(timerIntervall);
75+
}
76+
77+
private void AttachTimer(int intervall)
78+
{
6779
_eventTimer = new Timer
6880
{
69-
Interval = 50,
81+
Interval = intervall,
7082
AutoReset = false,
7183
Enabled = false
7284
};
@@ -333,5 +345,13 @@ public static void RunSpotifyWebHelper()
333345
Process.Start(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"spotify\spotifywebhelper.exe"));
334346
}
335347
}
348+
349+
public void Dispose()
350+
{
351+
if (_eventTimer == null)
352+
return;
353+
_eventTimer.Enabled = false;
354+
_eventTimer.Elapsed -= ElapsedTick;
355+
}
336356
}
337357
}

0 commit comments

Comments
 (0)