Skip to content

Commit d055781

Browse files
authored
Improve Task usage (#1172)
* Improve Task usage * Change log writer task to thread
1 parent 9f64562 commit d055781

File tree

5 files changed

+222
-242
lines changed

5 files changed

+222
-242
lines changed

src/PowerShellEditorServices.Hosting/Configuration/HostLogger.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ public static StreamLogger CreateWithNewFile(string path)
271271

272272
private readonly CancellationTokenSource _cancellationSource;
273273

274-
private readonly Task _writerTask;
274+
private readonly Thread _writerThread;
275275

276276
// This cannot be a bool
277277
// See https://stackoverflow.com/q/6164751
@@ -288,7 +288,8 @@ public StreamLogger(StreamWriter streamWriter)
288288
_messageQueue = new BlockingCollection<string>();
289289

290290
// Start writer listening to queue
291-
_writerTask = Task.Run(RunWriter);
291+
_writerThread = new Thread(RunWriter);
292+
_writerThread.Start();
292293
}
293294

294295
public void OnCompleted()
@@ -301,7 +302,7 @@ public void OnCompleted()
301302

302303
_cancellationSource.Cancel();
303304

304-
_writerTask.Wait();
305+
_writerThread.Join();
305306

306307
_unsubscriber.Dispose();
307308
_fileWriter.Flush();

src/PowerShellEditorServices/Services/Analysis/AnalysisService.cs

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -578,14 +578,9 @@ private PowerShellResult InvokePowerShell(string command, IDictionary<string, ob
578578
}
579579
}
580580

581-
private async Task<PowerShellResult> InvokePowerShellAsync(string command, IDictionary<string, object> paramArgMap = null)
581+
private Task<PowerShellResult> InvokePowerShellAsync(string command, IDictionary<string, object> paramArgMap = null)
582582
{
583-
var task = Task.Run(() =>
584-
{
585-
return InvokePowerShell(command, paramArgMap);
586-
});
587-
588-
return await task.ConfigureAwait(false);
583+
return Task.Run(() => InvokePowerShell(command, paramArgMap));
589584
}
590585

591586
/// <summary>
@@ -694,7 +689,7 @@ public PowerShellResult(
694689
public bool HasErrors { get; }
695690
}
696691

697-
internal async Task RunScriptDiagnosticsAsync(
692+
internal Task RunScriptDiagnosticsAsync(
698693
ScriptFile[] filesToAnalyze)
699694
{
700695
// If there's an existing task, attempt to cancel it
@@ -721,30 +716,22 @@ internal async Task RunScriptDiagnosticsAsync(
721716

722717
TaskCompletionSource<bool> cancelTask = new TaskCompletionSource<bool>();
723718
cancelTask.SetCanceled();
724-
return;
719+
return Task.CompletedTask;
725720
}
726721

727722
// If filesToAnalzye is empty, nothing to do so return early.
728723
if (filesToAnalyze.Length == 0)
729724
{
730-
return;
725+
return Task.CompletedTask;
731726
}
732727

733728
// Create a fresh cancellation token and then start the task.
734729
// We create this on a different TaskScheduler so that we
735730
// don't block the main message loop thread.
736731
// TODO: Is there a better way to do this?
737732
s_existingRequestCancellation = new CancellationTokenSource();
738-
await Task.Factory.StartNew(
739-
() =>
740-
DelayThenInvokeDiagnosticsAsync(
741-
750,
742-
filesToAnalyze,
743-
_configurationService.CurrentSettings.ScriptAnalysis.Enable ?? false,
744-
s_existingRequestCancellation.Token),
745-
CancellationToken.None,
746-
TaskCreationOptions.None,
747-
TaskScheduler.Default).ConfigureAwait(false);
733+
bool scriptAnalysisEnabled = _configurationService.CurrentSettings.ScriptAnalysis.Enable ?? false;
734+
return Task.Run(() => DelayThenInvokeDiagnosticsAsync(delayMilliseconds: 750, filesToAnalyze, scriptAnalysisEnabled, s_existingRequestCancellation.Token));
748735
}
749736

750737
private async Task DelayThenInvokeDiagnosticsAsync(

src/PowerShellEditorServices/Services/PowerShellContext/Console/WindowsConsoleOperations.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,12 @@ public async Task<ConsoleKeyInfo> ReadKeyAsync(bool intercept, CancellationToken
3737
await _readKeyHandle.WaitAsync(cancellationToken).ConfigureAwait(false);
3838
try
3939
{
40-
return
41-
_bufferedKey.HasValue
42-
? _bufferedKey.Value
43-
: await Task.Factory.StartNew(
44-
() => (_bufferedKey = System.Console.ReadKey(intercept)).Value);
40+
if (_bufferedKey == null)
41+
{
42+
_bufferedKey = await Task.Run(() => Console.ReadKey(intercept)).ConfigureAwait(false);
43+
}
44+
45+
return _bufferedKey.Value;
4546
}
4647
finally
4748
{

0 commit comments

Comments
 (0)