Skip to content

Commit

Permalink
Replace IProcess.WaitForProcessExitAsync with WaitForExitAsync
Browse files Browse the repository at this point in the history
kudos to @vjdw
  • Loading branch information
mstv committed Mar 15, 2023
1 parent 101eadd commit 70ebb97
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 17 deletions.
6 changes: 2 additions & 4 deletions GitCommands/Git/Executable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using GitExtUtils;
using GitUI;
using GitUIPluginInterfaces;
using Microsoft.VisualStudio.Threading;

namespace GitCommands
{
Expand Down Expand Up @@ -220,10 +221,7 @@ public StreamReader StandardError
public Task<int> WaitForExitAsync() => _exitTaskCompletionSource.Task;

/// <inheritdoc />
public Task WaitForProcessExitAsync(CancellationToken token)
{
return _process.WaitForExitAsync(token);
}
public Task<int> WaitForExitAsync(CancellationToken token) => WaitForExitAsync().WithCancellation(token);

/// <inheritdoc />
public int WaitForExit()
Expand Down
9 changes: 3 additions & 6 deletions GitCommands/Git/ExecutableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -318,16 +318,13 @@ string ComposeOutput()
}
#endif

// Wait for the process to exit (or be cancelled)
await process.WaitForProcessExitAsync(cancellationToken);

// Await the output and exit status
var exitTask = process.WaitForExitAsync();
// Wait for the process to exit (or be cancelled) and for the output
Task<int> exitTask = process.WaitForExitAsync(cancellationToken);
await Task.WhenAll(outputTask, errorTask, exitTask);

var output = outputEncoding.GetString(outputBuffer.GetBuffer(), 0, (int)outputBuffer.Length);
var error = outputEncoding.GetString(errorBuffer.GetBuffer(), 0, (int)errorBuffer.Length);
var exitCode = await process.WaitForExitAsync();
int exitCode = await exitTask;

if (cache is not null && exitCode == 0)
{
Expand Down
8 changes: 3 additions & 5 deletions Plugins/GitUIPluginInterfaces/IProcess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,10 @@ public interface IProcess : IDisposable
Task<int> WaitForExitAsync();

/// <summary>
/// Instructs the process component to wait for the associated process to exit, or for the cancellationToken to be cancelled (cancells the process).
/// Note that the exit task must be awaited too.
/// Returns a cancellable task that completes when the process exits, or when this object is disposed.
/// </summary>
/// <param name="token">An optional token to cancel the asynchronous operation.</param>
/// <returns>A task that will complete when the process has exited, cancellation has been requested, or an error occurs.</returns>
Task WaitForProcessExitAsync(CancellationToken token);
/// <returns>A task that yields the process's exit code, or <c>null</c> if this object was disposed before the process exited.</returns>
Task<int> WaitForExitAsync(CancellationToken token);

/// <summary>
/// Waits for the process to reach an idle state.
Expand Down
4 changes: 2 additions & 2 deletions UnitTests/CommonTestUtils/MockExecutable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ public Task<int> WaitForExitAsync()
}
}

public Task WaitForProcessExitAsync(CancellationToken token)
public Task<int> WaitForExitAsync(CancellationToken token)
{
return Task.CompletedTask;
return Task.FromResult(0);
}

public void WaitForInputIdle()
Expand Down

0 comments on commit 70ebb97

Please sign in to comment.