11
11
using GitHub . Extensions ;
12
12
using System . Threading . Tasks ;
13
13
using GitHub . Helpers ;
14
+ using System . Threading ;
14
15
15
16
namespace GitHub . Services
16
17
{
17
- public class UsageTracker : IUsageTracker
18
+ public sealed class UsageTracker : IUsageTracker , IDisposable
18
19
{
19
20
const string StoreFileName = "ghfvs.usage" ;
20
21
static readonly Calendar cal = CultureInfo . InvariantCulture . Calendar ;
21
22
22
23
readonly IGitHubServiceProvider gitHubServiceProvider ;
23
- readonly DispatcherTimer timer ;
24
-
25
24
IMetricsService client ;
26
25
IConnectionManager connectionManager ;
27
26
IPackageSettings userSettings ;
28
27
IVSServices vsservices ;
28
+ Timer timer ;
29
29
string storePath ;
30
30
bool firstRun = true ;
31
31
@@ -61,13 +61,16 @@ public UsageTracker(IGitHubServiceProvider gitHubServiceProvider)
61
61
} ;
62
62
dirCreate = ( path ) => System . IO . Directory . CreateDirectory ( path ) ;
63
63
64
- this . timer = new DispatcherTimer (
65
- TimeSpan . FromMinutes ( 3 ) ,
66
- DispatcherPriority . Background ,
64
+ this . timer = new Timer (
67
65
TimerTick ,
68
- ThreadingHelper . MainThreadDispatcher ) ;
66
+ null ,
67
+ TimeSpan . FromMinutes ( 3 ) ,
68
+ TimeSpan . FromHours ( 8 ) ) ;
69
+ }
69
70
70
- RunTimer ( ) ;
71
+ public void Dispose ( )
72
+ {
73
+ timer ? . Dispose ( ) ;
71
74
}
72
75
73
76
public async Task IncrementLaunchCount ( )
@@ -244,14 +247,7 @@ void SaveUsage(UsageStore store)
244
247
writeAllText ( storePath , json , Encoding . UTF8 ) ;
245
248
}
246
249
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 )
255
251
{
256
252
TimerTick ( )
257
253
. Catch ( ex =>
@@ -268,13 +264,13 @@ async Task TimerTick()
268
264
if ( firstRun )
269
265
{
270
266
await IncrementLaunchCount ( ) ;
271
- timer . Interval = TimeSpan . FromHours ( 8 ) ;
272
267
firstRun = false ;
273
268
}
274
269
275
270
if ( client == null || ! userSettings . CollectMetrics )
276
271
{
277
- timer . Stop ( ) ;
272
+ timer . Dispose ( ) ;
273
+ timer = null ;
278
274
return ;
279
275
}
280
276
0 commit comments