Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit 63fd51c

Browse files
authored
Merge pull request #960 from github/fixes/usagetracker-tick-bg-thread
Use Timer in usage tracker.
2 parents ea07d4f + f9e8838 commit 63fd51c

File tree

1 file changed

+14
-18
lines changed

1 file changed

+14
-18
lines changed

src/GitHub.VisualStudio/Services/UsageTracker.cs

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,21 @@
1111
using GitHub.Extensions;
1212
using System.Threading.Tasks;
1313
using GitHub.Helpers;
14+
using System.Threading;
1415

1516
namespace 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

Comments
 (0)