Skip to content

Commit 04de57c

Browse files
committed
Executed the command in a background thread.
1 parent f7f2ba2 commit 04de57c

1 file changed

Lines changed: 24 additions & 7 deletions

File tree

src/GtmExtension/GtmListener.cs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@
1111
using System;
1212
using System.ComponentModel;
1313
using System.ComponentModel.Composition;
14+
using System.Diagnostics;
15+
using System.IO;
16+
using System.Runtime.CompilerServices;
1417
using System.Windows;
1518
using Process = System.Diagnostics.Process;
19+
using Task = System.Threading.Tasks.Task;
1620

1721
namespace GtmExtension
1822
{
@@ -239,7 +243,7 @@ private void Initialize()
239243
documentEvents = dte.Events.DocumentEvents;
240244
documentEvents.DocumentSaved += DocumentEvents_DocumentSaved;
241245
}
242-
private void Update(string path, bool force = false)
246+
private void Update(string path, bool force = false, [CallerMemberName] string caller = null)
243247
{
244248
if (path == null) { return; }
245249

@@ -250,13 +254,26 @@ private void Update(string path, bool force = false)
250254
time - lastUpdate >= updateInterval ||
251255
path != prevPath)
252256
{
253-
status = ExecuteForOutput(gtmExe, $"record --status \"{path}\"");
254-
if (!string.IsNullOrWhiteSpace(status))
255-
{
256-
AppendToWindowTitle($"[GTM: {status}]");
257-
}
258-
259257
prevPath = path;
258+
#if DEBUG
259+
var sw = Stopwatch.StartNew();
260+
#endif
261+
Task.Run(async () =>
262+
{
263+
// This will run in background thread.
264+
status = ExecuteForOutput(gtmExe, $"record --status \"{path}\"");
265+
266+
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
267+
268+
if (!string.IsNullOrWhiteSpace(status))
269+
{
270+
AppendToWindowTitle($"[GTM: {status}]");
271+
}
272+
#if DEBUG
273+
sw.Stop();
274+
Debug.WriteLine($"Updated {Path.GetFileName(path)} from {caller} in {sw.Elapsed}.");
275+
#endif
276+
});
260277
}
261278
else if (!string.IsNullOrWhiteSpace(status))
262279
{

0 commit comments

Comments
 (0)