Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions CliWrap/Builders/ResourcePolicyBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ namespace CliWrap.Builders;
/// </summary>
public class ResourcePolicyBuilder
{
private ProcessPriorityClass _priority = ProcessPriorityClass.Normal;
private ProcessPriorityClass? _priority;
private nint? _affinity;
private nint? _minWorkingSet;
private nint? _maxWorkingSet;

/// <summary>
/// Sets the priority class of the process.
/// </summary>
public ResourcePolicyBuilder SetPriority(ProcessPriorityClass priority)
public ResourcePolicyBuilder SetPriority(ProcessPriorityClass? priority)
{
_priority = priority;
return this;
Expand Down
5 changes: 3 additions & 2 deletions CliWrap/Command.Execution.cs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,8 @@ CancellationToken gracefulCancellationToken
{
// Disable CA1416 because we're handling an exception that is thrown by the property setters
#pragma warning disable CA1416
p.PriorityClass = ResourcePolicy.Priority;
if (ResourcePolicy.Priority is not null)
p.PriorityClass = ResourcePolicy.Priority.Value;

if (ResourcePolicy.Affinity is not null)
p.ProcessorAffinity = ResourcePolicy.Affinity.Value;
Expand All @@ -332,7 +333,7 @@ CancellationToken gracefulCancellationToken
catch (NotSupportedException ex)
{
throw new NotSupportedException(
"Cannot set resource policy for the process. "
"Cannot start a process with the provided resource policy. "
+ "Setting custom priority, affinity, and/or working set limits is not supported on this platform.",
ex
);
Expand Down
4 changes: 2 additions & 2 deletions CliWrap/ResourcePolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace CliWrap;
/// Resource policy assigned to a process.
/// </summary>
public partial class ResourcePolicy(
ProcessPriorityClass priority = ProcessPriorityClass.Normal,
ProcessPriorityClass? priority = null,
nint? affinity = null,
nint? minWorkingSet = null,
nint? maxWorkingSet = null
Expand All @@ -15,7 +15,7 @@ public partial class ResourcePolicy(
/// <summary>
/// Priority class of the process.
/// </summary>
public ProcessPriorityClass Priority { get; } = priority;
public ProcessPriorityClass? Priority { get; } = priority;

/// <summary>
/// Processor core affinity mask of the process.
Expand Down
21 changes: 14 additions & 7 deletions CliWrap/Utils/ProcessEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ public void Start(Action<Process>? configureProcess = null)
_nativeProcess.EnableRaisingEvents = true;
_nativeProcess.Exited += (_, _) =>
{
// Record exit time
ExitTime = DateTimeOffset.Now;

// Release the waiting task
_exitTcs.TrySetResult(null);
};

Expand All @@ -59,23 +62,25 @@ public void Start(Action<Process>? configureProcess = null)
{
throw new InvalidOperationException(
$"Failed to start a process with file path '{_nativeProcess.StartInfo.FileName}'. "
+ "Target file is not an executable or lacks execute permissions."
+ "Target file is not an executable or lacks the 'execute' permission."
);
}

StartTime = DateTimeOffset.Now;

// Apply custom configurations
configureProcess?.Invoke(_nativeProcess);
}
catch (Win32Exception ex)
{
throw new Win32Exception(
$"Failed to start a process with file path '{_nativeProcess.StartInfo.FileName}'. "
+ "Target file or working directory doesn't exist, or the provided credentials are invalid.",
+ "Target file or working directory doesn't exist, the provided credentials are invalid, or the resource policy cannot be set due to insufficient permissions. "
+ "See the inner exception for more information.",
ex
);
}

// Record start time
StartTime = DateTimeOffset.Now;

// Apply custom configurations
configureProcess?.Invoke(_nativeProcess);
}

// Sends SIGINT
Expand Down Expand Up @@ -148,7 +153,9 @@ public async Task WaitUntilExitAsync(CancellationToken cancellationToken = defau
.Register(() => _exitTcs.TrySetCanceled(cancellationToken))
.ToAsyncDisposable()
)
{
await _exitTcs.Task.ConfigureAwait(false);
}
}

public void Dispose() => _nativeProcess.Dispose();
Expand Down