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 ( )
@@ -192,6 +195,13 @@ public async Task IncrementGitHubPaneHelpClicks()
192195 SaveUsage ( usage ) ;
193196 }
194197
198+ public async Task IncrementPullRequestOpened ( )
199+ {
200+ var usage = await LoadUsage ( ) ;
201+ ++ usage . Model . NumberOfPullRequestsOpened ;
202+ SaveUsage ( usage ) ;
203+ }
204+
195205 async Task Initialize ( )
196206 {
197207 // The services needed by the usage tracker are loaded when they are first needed to
@@ -244,14 +254,7 @@ void SaveUsage(UsageStore store)
244254 writeAllText ( storePath , json , Encoding . UTF8 ) ;
245255 }
246256
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 )
257+ void TimerTick ( object state )
255258 {
256259 TimerTick ( )
257260 . Catch ( ex =>
@@ -268,13 +271,13 @@ async Task TimerTick()
268271 if ( firstRun )
269272 {
270273 await IncrementLaunchCount ( ) ;
271- timer . Interval = TimeSpan . FromHours ( 8 ) ;
272274 firstRun = false ;
273275 }
274276
275277 if ( client == null || ! userSettings . CollectMetrics )
276278 {
277- timer . Stop ( ) ;
279+ timer . Dispose ( ) ;
280+ timer = null ;
278281 return ;
279282 }
280283
@@ -350,6 +353,9 @@ static void ClearCounters(UsageModel usage, bool weekly, bool monthly)
350353 usage . NumberOfForkPullRequestsCheckedOut = 0 ;
351354 usage . NumberOfForkPullRequestPulls = 0 ;
352355 usage . NumberOfForkPullRequestPushes = 0 ;
356+ usage . NumberOfGitHubPaneHelpClicks = 0 ;
357+ usage . NumberOfWelcomeTrainingClicks = 0 ;
358+ usage . NumberOfWelcomeDocsClicks = 0 ;
353359
354360 if ( weekly )
355361 usage . NumberOfStartupsWeek = 0 ;
0 commit comments