All checks were successful
release / dotnet-release-workflow (push) Successful in 1m23s
94 lines
No EOL
2 KiB
C#
94 lines
No EOL
2 KiB
C#
// Copyright (c) The Geekeey Authors
|
|
// SPDX-License-Identifier: EUPL-1.2
|
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
namespace Geekeey.Process;
|
|
|
|
/// <summary>
|
|
/// Represents an asynchronous execution of a command.
|
|
/// </summary>
|
|
public sealed partial class CommandTask<TResult> : IDisposable
|
|
{
|
|
private readonly Process _process;
|
|
|
|
internal CommandTask(Task<TResult> task, Process process, int processId)
|
|
{
|
|
Task = task;
|
|
_process = process;
|
|
ProcessId = processId;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Underlying task.
|
|
/// </summary>
|
|
public Task<TResult> Task { get; }
|
|
|
|
/// <summary>
|
|
/// Underlying process ID.
|
|
/// </summary>
|
|
public int ProcessId { get; }
|
|
|
|
internal CommandTask<T> Bind<T>(Func<Task<TResult>, Task<T>> transform)
|
|
{
|
|
return new CommandTask<T>(transform(Task), _process, ProcessId);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Lazily maps the result of the task using the specified transform.
|
|
/// </summary>
|
|
internal CommandTask<T> Select<T>(Func<TResult, T> transform)
|
|
{
|
|
return Bind(async task => transform(await task));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Signals the process with an interrupt request from the keyboard.
|
|
/// </summary>
|
|
public void Interrupt()
|
|
{
|
|
_process.Interrupt();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Immediately stops the associated process and its descendent processes.
|
|
/// </summary>
|
|
public void Kill()
|
|
{
|
|
_process.Kill();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the awaiter of the underlying task.
|
|
/// Used to enable await expressions on this object.
|
|
/// </summary>
|
|
public TaskAwaiter<TResult> GetAwaiter()
|
|
{
|
|
return Task.GetAwaiter();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Configures an awaiter used to await this task.
|
|
/// </summary>
|
|
public ConfiguredTaskAwaitable<TResult> ConfigureAwait(bool continueOnCapturedContext)
|
|
{
|
|
return Task.ConfigureAwait(continueOnCapturedContext);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public void Dispose()
|
|
{
|
|
Task.Dispose();
|
|
}
|
|
}
|
|
|
|
public sealed partial class CommandTask<TResult>
|
|
{
|
|
/// <summary>
|
|
/// Converts the command task into a regular task.
|
|
/// </summary>
|
|
public static implicit operator Task<TResult>(CommandTask<TResult> commandTask)
|
|
{
|
|
return commandTask.Task;
|
|
}
|
|
} |