1111using GitHub . Extensions ;
1212using System . Threading . Tasks ;
1313using GitHub . Helpers ;
14+ using System . Threading ;
1415
1516namespace GitHub . Services
1617{
17- public class UsageTracker : IUsageTracker
18+ public sealed class UsageTracker : IUsageTracker , IDisposable
1819 {
1920 const string StoreFileName = "ghfvs.usage" ;
2021 static readonly Calendar cal = CultureInfo . InvariantCulture . Calendar ;
2122
2223 readonly IGitHubServiceProvider gitHubServiceProvider ;
23- readonly DispatcherTimer timer ;
24-
2524 IMetricsService client ;
2625 IConnectionManager connectionManager ;
2726 IPackageSettings userSettings ;
2827 IVSServices vsservices ;
28+ Timer timer ;
2929 string storePath ;
3030 bool firstRun = true ;
3131
@@ -61,13 +61,16 @@ public UsageTracker(IGitHubServiceProvider gitHubServiceProvider)
6161 } ;
6262 dirCreate = ( path ) => System . IO . Directory . CreateDirectory ( path ) ;
6363
64- this . timer = new DispatcherTimer (
65- TimeSpan . FromMinutes ( 3 ) ,
66- DispatcherPriority . Background ,
64+ this . timer = new Timer (
6765 TimerTick ,
68- ThreadingHelper . MainThreadDispatcher ) ;
66+ null ,
67+ TimeSpan . FromMinutes ( 3 ) ,
68+ TimeSpan . FromHours ( 8 ) ) ;
69+ }
6970
70- RunTimer ( ) ;
71+ public void Dispose ( )
72+ {
73+ timer ? . Dispose ( ) ;
7174 }
7275
7376 public async Task IncrementLaunchCount ( )
@@ -244,14 +247,7 @@ void SaveUsage(UsageStore store)
244247 writeAllText ( storePath , json , Encoding . UTF8 ) ;
245248 }
246249
247- void RunTimer ( )
248- {
249- // The timer first ticks after 3 minutes to allow things to settle down after startup.
250- // This will be changed to 8 hours after the first tick by the TimerTick method.
251- timer . Start ( ) ;
252- }
253-
254- void TimerTick ( object sender , EventArgs e )
250+ void TimerTick ( object state )
255251 {
256252 TimerTick ( )
257253 . Catch ( ex =>
@@ -268,13 +264,13 @@ async Task TimerTick()
268264 if ( firstRun )
269265 {
270266 await IncrementLaunchCount ( ) ;
271- timer . Interval = TimeSpan . FromHours ( 8 ) ;
272267 firstRun = false ;
273268 }
274269
275270 if ( client == null || ! userSettings . CollectMetrics )
276271 {
277- timer . Stop ( ) ;
272+ timer . Dispose ( ) ;
273+ timer = null ;
278274 return ;
279275 }
280276
0 commit comments