ProcessExtensions
Provides extension methods for the Process class to manage processor affinity, memory limits, and job behavior.
方法
public static int Execute(string fileName, string arg, Action<string> output, Action<string> error, Action<ProcessStartInfo> infoHandler, TimeSpan waitForExit, string stdInput, ProcessPriorityClass? priority)
result = ProcessExtensions.Execute(fileName, arg, output, error, infoHandler, waitForExit, stdInput, priority)
Executes a process with specified arguments and output handlers.
- fileName
- The process file name.
- arg
- Arguments for the process.
- output
- Action to handle standard output.
- error
- Action to handle standard error.
- infoHandler
- Optional action to modify process start info.
- waitForExit
- TimeSpan to wait for process exit.
- stdInput
- Standard input to write to the process.
- priority
- Optional process priority.
返回值: The process exit code.
public static Task<int> ExecuteAsync(string fileName, string arg, Action<string> output, Action<string> error, Action<ProcessStartInfo> infoHandler, string stdInput, ProcessPriorityClass? priority, CancellationToken cancellationToken)
result = ProcessExtensions.ExecuteAsync(fileName, arg, output, error, infoHandler, stdInput, priority, cancellationToken)
Asynchronously executes a process with specified arguments and output handlers.
- fileName
- The file name to execute.
- arg
- Arguments for the process.
- output
- Action to handle standard output.
- error
- Action to handle standard error.
- infoHandler
- Optional action to modify process start info.
- stdInput
- Standard input to send to the process.
- priority
- Optional process priority.
- cancellationToken
- A cancellation token.
返回值: A task that represents the asynchronous operation, containing the process exit code.
public static SafeFileHandle LimitByMemory(Process process, long limit)
result = ProcessExtensions.LimitByMemory(process, limit)
Limits the process memory usage. Windows: Job Object. Linux: setrlimit(RLIMIT_AS) for current process only.
- process
- The process to limit memory usage for.
- limit
- The maximum allowed memory in bytes.
返回值: A SafeFileHandle representing the job object that manages the memory limit.
public static bool OpenLink(string url, bool raiseError)
result = ProcessExtensions.OpenLink(url, raiseError)
Opens the specified URL or file path using the default system launcher.
- url
- The URL or file path to open.
- raiseError
- Determines if an exception should be raised if opening fails.
返回值: True if the operation is successful; otherwise, false.
public static SafeFileHandle SetKillChildsOnClose(Process process)
result = ProcessExtensions.SetKillChildsOnClose(process)
Configures the process to automatically kill its child processes when the process is closed by assigning it to a Windows Job Object with the kill-on-job-close flag.
- process
- The process whose child processes should be terminated on close.
返回值: A SafeFileHandle representing the job object that enforces the kill-on-job-close behavior.
public static void SetProcessorAffinity(Process process, long cpu)
ProcessExtensions.SetProcessorAffinity(process, cpu)
Sets the processor affinity for the process, allowing control over which CPU cores the process can execute on.
- process
- The process whose processor affinity is to be set.
- cpu
- The bitmask representing the allowed CPU cores. The mask is applied to the process's current affinity.