JobObject Class
- Supported on windows5.1.2600
A utility class that represents a Windows job object. Job objects allow groups of processes to be managed as a unit.
[SupportedOSPlatform("windows5.1.2600")]
public sealed class JobObject : IDisposableExample
using var job = new JobObject();
job.SetLimits(new JobObjectLimits
{
Flags = JobObjectLimitFlags.KillOnJobClose,
});
// Start a child process and assign it to the job
using var childProcess = Process.Start(new ProcessStartInfo("ping", "127.0.0.1 -n 100") { CreateNoWindow = true })!;
job.AssignProcess(childProcess);
// Disposing the job terminates the child process
job.Dispose();
childProcess.WaitForExit();- Implements
Constructors
| Name | Description |
|---|---|
| JobObject() | Initializes a new instance of the JobObject class. The associated job object will have no name. |
| JobObject(string?) | Initializes a new instance of the JobObject class. |
| JobObject(string?, bool) | Initializes a new instance of the JobObject class. |
JobObject overloads
JobObject()
JobObject.csInitializes a new instance of the JobObject class. The associated job object will have no name.
public JobObject()JobObject(string?)
JobObject.csJobObject(string?, bool)
JobObject.csInitializes a new instance of the JobObject class.
Parameters
namestring?- The job object name. May be null.
inheritHandlebool- A Boolean value that specifies whether the returned handle is inherited when a new process is created. If this member is
true, the new process inherits the handle.
Remarks
When name matches a job object that already exists, this attaches to that job object instead of creating a new one. Inspect JobObject.CreatedNew to tell the two apart.
Properties
| Name | Description |
|---|---|
| CreatedNew | Gets a value indicating whether this instance created the underlying job object. |
CreatedNew
JobObject.csGets a value indicating whether this instance created the underlying job object.
public bool CreatedNew { get; }Property value
bool true when the constructor created a new job object; false when a job object with the requested name already existed and this instance attached to it, or when the instance was returned by JobObject.Open or JobObject.TryOpen.
Remarks
Job objects share a namespace with events, semaphores, mutexes, waitable timers and file-mapping objects, and CreateJobObject returns a handle to the existing object rather than failing when the name is taken. Check this property when the caller needs an isolated job: attaching to a job created elsewhere means inheriting its limits, its process set and its lifetime.
Methods
| Name | Description |
|---|---|
| AssignProcess(Process) | Assigns a process to an existing job object. |
| AssignProcess(nint) | Assigns a process to an existing job object. |
| DisableCpuRateHardCap() | Disables the job's CPU rate limit. |
| Dispose() | |
| GetBasicAccountingInformation() | Get the job's basic accounting information. |
| GetBasicAndIoAccountingInformation() | Get the job's basic and IO accounting information. |
| GetCpuRateHardCap() | Get the job's CPU rate control policy and the values that belong to it. |
| GetMemoryAccountingInformation() | Get the job's memory accounting information. |
| IsAssignedToProcess(Process) | Determines whether the specified process is assigned to this job object. |
| Open(JobObjectAccessRights, bool, string) | Opens an existing job object. |
| SetCpuRate(int, int) | Set the job's CPU rate is a hard limit. After the job reaches its CPU cycle limit for the current scheduling interval, no threads associated with the job will run until the next interval. |
| SetCpuRateHardCap(int) | Set the job's CPU rate is a hard limit. After the job reaches its CPU cycle limit for the current scheduling interval, no threads associated with the job will run until the next interval. |
| SetCpuRateWeight(int) | Set the job's CPU rate is calculated based on its relative weight to the weight of other jobs. |
| SetIoLimits(JobIoRateLimits) | Sets I/O limits on a job object. |
| SetLimits(JobObjectLimits) | Sets limits to the job. |
| SetNetRateLimits(ulong) | Set bandwidth limits for the job. |
| SetSecurityLimits(JobObjectSecurityLimit) | Set security limits for the job. |
| SetUIRestrictions(JobObjectUILimit) | Sets UI restrictions for processes in the job. |
| Terminate() | Terminates all processes currently associated with the job. If the job is nested, this function terminates all processes currently associated with the job and all of its child jobs in the hierarchy. |
| Terminate(int) | Terminates all processes currently associated with the job. If the job is nested, this function terminates all processes currently associated with the job and all of its child jobs in the hierarchy. |
| TryOpen(JobObjectAccessRights, bool, string, JobObject?) | Attempts to open an existing job object. |
AssignProcess overloads
AssignProcess(Process)
JobObject.csAssignProcess(nint)
JobObject.csAssigns a process to an existing job object.
Parameters
processHandlenint- The process handle.
Remarks
The caller must keep processHandle open for the duration of the call. Prefer JobObject.AssignProcess when a Process instance is available.
DisableCpuRateHardCap()
JobObject.csDisables the job's CPU rate limit.
public void DisableCpuRateHardCap()Dispose()
JobObject.cspublic void Dispose()No documentation is available.
GetBasicAccountingInformation()
JobObject.csGet the job's basic accounting information.
public JobObjectBasicAccountingInformation GetBasicAccountingInformation()Returns
JobObjectBasicAccountingInformation JobObjectBasicAccountingInformation record containing basic accounting information.
Exceptions
Win32Exception- The operation failed.
GetBasicAndIoAccountingInformation()
JobObject.csGet the job's basic and IO accounting information.
public JobObjectBasicAndIoAccountingInformation GetBasicAndIoAccountingInformation()Returns
JobObjectBasicAndIoAccountingInformation JobObjectBasicAndIoAccountingInformation record containing both basic and IO accounting information.
Exceptions
Win32Exception- The operation failed.
GetCpuRateHardCap()
JobObject.csGet the job's CPU rate control policy and the values that belong to it.
public JobObjectCpuHardCap GetCpuRateHardCap()Returns
JobObjectCpuHardCap A JobObjectCpuHardCap describing the active policy. Windows stores the rate, the weight and the min/max pair in a union, so only the members matching JobObjectCpuHardCap.Mode carry a meaningful value.
Exceptions
Win32Exception- The operation failed.
GetMemoryAccountingInformation()
JobObject.csGet the job's memory accounting information.
public JobObjectMemoryAccountingInformation GetMemoryAccountingInformation()Returns
JobObjectMemoryAccountingInformation JobObjectMemoryAccountingInformation record containing memory accounting information.
Exceptions
Win32Exception- The operation failed.
IsAssignedToProcess(Process)
JobObject.csDetermines whether the specified process is assigned to this job object.
Parameters
processProcess- The process to check.
Returns
bool true if the process is assigned to the job object; otherwise, false.
Exceptions
ArgumentNullExceptionprocessisnull.Win32Exception- The operation failed.
Open(JobObjectAccessRights, bool, string)
JobObject.csOpens an existing job object.
public static JobObject Open(JobObjectAccessRights desiredAccess, bool inherited, string name)Parameters
desiredAccessJobObjectAccessRights- The access rights for the job object.
inheritedbool- If this value is true, processes created by this process will inherit the handle. Otherwise, the processes do not inherit this handle.
namestring- The name of the job object to be opened.
Returns
JobObject A JobObject instance representing the opened job object.
Exceptions
Win32Exception- The job object could not be opened.
SetCpuRate(int, int)
JobObject.csSet the job's CPU rate is a hard limit. After the job reaches its CPU cycle limit for the current scheduling interval, no threads associated with the job will run until the next interval.
Parameters
minRateint- Specifies the minimum portion of the processor cycles that the threads in a job object can reserve during each scheduling interval. Specify this rate as a percentage times 100. For example, to set a minimum rate of 50%, specify 50 times 100, or 5,000.
maxRateint- Specifies the maximum portion of processor cycles that the threads in a job object can use during each scheduling interval. Specify this rate as a percentage times 100. For example, to set a maximum rate of 50%, specify 50 times 100, or 5,000. After the job reaches this limit for a scheduling interval, no threads associated with the job can run until the next scheduling interval.
Exceptions
ArgumentOutOfRangeExceptionminRateormaxRateis not between 1 and 10,000, orminRateis greater thanmaxRate.
SetCpuRateHardCap(int)
JobObject.csSet the job's CPU rate is a hard limit. After the job reaches its CPU cycle limit for the current scheduling interval, no threads associated with the job will run until the next interval.
Parameters
cpuRateint- Specifies the portion of processor cycles that the threads in a job object can use during each scheduling interval, as the number of cycles per 10,000 cycles. For example, to let the job use 20% of the CPU, set CpuRate to 20 times 100, or 2,000.
Exceptions
ArgumentOutOfRangeExceptioncpuRateis not between 1 and 10,000.
SetCpuRateWeight(int)
JobObject.csSet the job's CPU rate is calculated based on its relative weight to the weight of other jobs.
Parameters
weightint- Specifies the scheduling weight of the job object, which determines the share of processor time given to the job relative to other workloads on the processor. This member can be a value from 1 through 9, where 1 is the smallest share and 9 is the largest share.The default is 5, which should be used for most workloads.
Exceptions
ArgumentOutOfRangeExceptionweightis not between 1 and 9.
SetIoLimits(JobIoRateLimits)
JobObject.cs- Supported on windows10.0.10240
Sets I/O limits on a job object.
[SupportedOSPlatform("windows10.0.10240")]
public void SetIoLimits(JobIoRateLimits limits)Parameters
limitsJobIoRateLimits
SetLimits(JobObjectLimits)
JobObject.csSets limits to the job.
public void SetLimits(JobObjectLimits limits)Parameters
limitsJobObjectLimits- The limits. May not be null.
SetNetRateLimits(ulong)
JobObject.csSetSecurityLimits(JobObjectSecurityLimit)
JobObject.csSet security limits for the job.
public void SetSecurityLimits(JobObjectSecurityLimit securityLimit)Parameters
securityLimitJobObjectSecurityLimit
SetUIRestrictions(JobObjectUILimit)
JobObject.csSets UI restrictions for processes in the job.
public void SetUIRestrictions(JobObjectUILimit limits)Parameters
limitsJobObjectUILimit- The UI restrictions to apply to the job.
Exceptions
Win32Exception- The UI restrictions could not be set.
Terminate overloads
Terminate()
JobObject.csTerminates all processes currently associated with the job. If the job is nested, this function terminates all processes currently associated with the job and all of its child jobs in the hierarchy.
public void Terminate()Exceptions
Win32Exception- The processes could not be terminated.
Terminate(int)
JobObject.csTerminates all processes currently associated with the job. If the job is nested, this function terminates all processes currently associated with the job and all of its child jobs in the hierarchy.
Parameters
exitCodeint- The exit code to be used by all processes and threads in the job object.
Exceptions
Win32Exception- The processes could not be terminated. This happens when the handle does not have the
JobObjectAccessRights.Terminateaccess right.
TryOpen(JobObjectAccessRights, bool, string, JobObject?)
JobObject.csAttempts to open an existing job object.
public static bool TryOpen(JobObjectAccessRights desiredAccess, bool inherited, string name, out JobObject? jobObject)Parameters
desiredAccessJobObjectAccessRights- The access rights for the job object.
inheritedbool- If this value is true, processes created by this process will inherit the handle. Otherwise, the processes do not inherit this handle.
namestring- The name of the job object to be opened.
jobObjectJobObject?- When this method returns, contains the opened job object if the operation succeeded, or null if the job object was not found.
Returns
bool true if the job object was successfully opened; otherwise, false.
Exceptions
Win32Exception- An error occurred while attempting to open the job object (other than the job object not being found).