Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Task usage #1172

Merged
merged 5 commits into from
Feb 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ public static StreamLogger CreateWithNewFile(string path)

private readonly CancellationTokenSource _cancellationSource;

private readonly Task _writerTask;
private readonly Thread _writerThread;

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

// Start writer listening to queue
_writerTask = Task.Run(RunWriter);
_writerThread = new Thread(RunWriter);
_writerThread.Start();
}

public void OnCompleted()
Expand All @@ -301,7 +302,7 @@ public void OnCompleted()

_cancellationSource.Cancel();

_writerTask.Wait();
_writerThread.Join();

_unsubscriber.Dispose();
_fileWriter.Flush();
Expand Down
27 changes: 7 additions & 20 deletions src/PowerShellEditorServices/Services/Analysis/AnalysisService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -578,14 +578,9 @@ private PowerShellResult InvokePowerShell(string command, IDictionary<string, ob
}
}

private async Task<PowerShellResult> InvokePowerShellAsync(string command, IDictionary<string, object> paramArgMap = null)
private Task<PowerShellResult> InvokePowerShellAsync(string command, IDictionary<string, object> paramArgMap = null)
{
var task = Task.Run(() =>
{
return InvokePowerShell(command, paramArgMap);
});

return await task.ConfigureAwait(false);
return Task.Run(() => InvokePowerShell(command, paramArgMap));
}

/// <summary>
Expand Down Expand Up @@ -694,7 +689,7 @@ private class PowerShellResult
public bool HasErrors { get; }
}

internal async Task RunScriptDiagnosticsAsync(
internal Task RunScriptDiagnosticsAsync(
ScriptFile[] filesToAnalyze)
{
// If there's an existing task, attempt to cancel it
Expand All @@ -721,30 +716,22 @@ private class PowerShellResult

TaskCompletionSource<bool> cancelTask = new TaskCompletionSource<bool>();
cancelTask.SetCanceled();
return;
return Task.CompletedTask;
}

// If filesToAnalzye is empty, nothing to do so return early.
if (filesToAnalyze.Length == 0)
{
return;
return Task.CompletedTask;
}

// Create a fresh cancellation token and then start the task.
// We create this on a different TaskScheduler so that we
// don't block the main message loop thread.
// TODO: Is there a better way to do this?
s_existingRequestCancellation = new CancellationTokenSource();
await Task.Factory.StartNew(
() =>
DelayThenInvokeDiagnosticsAsync(
750,
filesToAnalyze,
_configurationService.CurrentSettings.ScriptAnalysis.Enable ?? false,
s_existingRequestCancellation.Token),
CancellationToken.None,
TaskCreationOptions.None,
TaskScheduler.Default).ConfigureAwait(false);
bool scriptAnalysisEnabled = _configurationService.CurrentSettings.ScriptAnalysis.Enable ?? false;
return Task.Run(() => DelayThenInvokeDiagnosticsAsync(delayMilliseconds: 750, filesToAnalyze, scriptAnalysisEnabled, s_existingRequestCancellation.Token));
}

private async Task DelayThenInvokeDiagnosticsAsync(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@ public async Task<ConsoleKeyInfo> ReadKeyAsync(bool intercept, CancellationToken
await _readKeyHandle.WaitAsync(cancellationToken).ConfigureAwait(false);
try
{
return
_bufferedKey.HasValue
? _bufferedKey.Value
: await Task.Factory.StartNew(
() => (_bufferedKey = System.Console.ReadKey(intercept)).Value);
if (_bufferedKey == null)
{
_bufferedKey = await Task.Run(() => Console.ReadKey(intercept)).ConfigureAwait(false);
}

return _bufferedKey.Value;
}
finally
{
Expand Down