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 1 commit
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 @@ -288,7 +288,11 @@ public StreamLogger(StreamWriter streamWriter)
_messageQueue = new BlockingCollection<string>();

// Start writer listening to queue
_writerTask = Task.Run(RunWriter);
_writerTask = Task.Factory.StartNew(
RunWriter,
CancellationToken.None, // Inner method will manage cancellation
TaskCreationOptions.LongRunning | TaskCreationOptions.DenyChildAttach,
TaskScheduler.Default);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this never exits, I'd just throw it in a Thread like you mentioned.

}

public void OnCompleted()
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,7 @@ 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);
return _bufferedKey ?? await Task.Run(() => (_bufferedKey = System.Console.ReadKey(intercept)).Value).ConfigureAwait(false);
}
finally
{
Expand Down