public Job() { handle = CreateJobObject(IntPtr.Zero, null); if (handle.IsInvalid) { throw new Win32Exception(); } var extendedInfo = new JOBOBJECT_EXTENDED_LIMIT_INFORMATION { BasicLimitInformation = new JOBOBJECT_BASIC_LIMIT_INFORMATION { LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE } }; int cbJobObjectInfo = Marshal.SizeOf(typeof(JOBOBJECT_EXTENDED_LIMIT_INFORMATION)); IntPtr lpJobObjectInfo = Marshal.AllocHGlobal(cbJobObjectInfo); try { Marshal.StructureToPtr(extendedInfo, lpJobObjectInfo, false); if (!SetInformationJobObject(handle, JobObjectInfoType.ExtendedLimitInformation, ref extendedInfo, cbJobObjectInfo)) { throw new Win32Exception(); } } finally { Marshal.FreeHGlobal(lpJobObjectInfo); } }
public ChildProcessManager(ulong memoryLimit) { _handle = new SafeJobHandle(CreateJobObject(IntPtr.Zero, null)); var info = new JOBOBJECT_BASIC_LIMIT_INFORMATION { LimitFlags = 0x00000100 }; var extendedInfo = new JOBOBJECT_EXTENDED_LIMIT_INFORMATION { BasicLimitInformation = info, ProcessMemoryLimit = (UIntPtr)memoryLimit }; var length = Marshal.SizeOf(typeof(JOBOBJECT_EXTENDED_LIMIT_INFORMATION)); var extendedInfoPtr = Marshal.AllocHGlobal(length); Marshal.StructureToPtr(extendedInfo, extendedInfoPtr, false); if (!SetInformationJobObject(_handle, JobObjectInfoType.ExtendedLimitInformation, extendedInfoPtr, (uint)length)) { throw new InvalidOperationException($"Unable to set information. Error: {Marshal.GetLastWin32Error()}"); } }
public WindowsProcessJob(Process process) { IntPtr newHandle = Native.CreateJobObject(IntPtr.Zero, null); if (newHandle == IntPtr.Zero) { throw new InvalidOperationException("Unable to create a job. Error: " + Marshal.GetLastWin32Error()); } this.jobHandle = new SafeJobHandle(newHandle); Native.JOBOBJECT_BASIC_LIMIT_INFORMATION info = new Native.JOBOBJECT_BASIC_LIMIT_INFORMATION { LimitFlags = 0x2000 // JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE }; Native.JOBOBJECT_EXTENDED_LIMIT_INFORMATION extendedInfo = new Native.JOBOBJECT_EXTENDED_LIMIT_INFORMATION { BasicLimitInformation = info }; int length = Marshal.SizeOf(typeof(Native.JOBOBJECT_EXTENDED_LIMIT_INFORMATION)); if (!Native.SetInformationJobObject(this.jobHandle, Native.JobObjectInfoType.ExtendedLimitInformation, ref extendedInfo, (uint)length)) { throw new InvalidOperationException("Unable to configure the job. Error: " + Marshal.GetLastWin32Error()); } if (!Native.AssignProcessToJobObject(this.jobHandle, process.Handle)) { throw new InvalidOperationException("Unable to add process to the job. Error: " + Marshal.GetLastWin32Error()); } }
public void Dispose() { if (_disposed) return; _handle.Dispose(); _handle = null; _disposed = true; }
public static unsafe NativeJob Create(string jobName) { SafeJobHandle handle = CreateJobObjectW(null, jobName); if (handle.IsInvalid) { throw new Win32Exception(); } return(new NativeJob(handle)); }
private void Dispose(bool disposing) { if (!this.disposed) { this.jobHandle.Dispose(); this.jobHandle = null; this.disposed = true; } }
public static unsafe NativeJob Open(string jobName, JobAccessRights accessRights = JobAccessRights.All, bool inheritHandle = false) { SafeJobHandle handle = OpenJobObjectW((uint)accessRights, inheritHandle, jobName); if (handle.IsInvalid) { throw new Win32Exception(); } return(new NativeJob(handle)); }
public void Dispose() { if (_disposed) { return; } _handle.Dispose(); _handle = null; _disposed = true; }
public void Dispose() { if (_disposed) { return; } SetHandle(NativeMethods.JobObjectLimitFlags.JOB_NONE); _handle.Close(); _handle.Dispose(); _handle = null; _disposed = true; }
public void Dispose() { if (m_disposed) { return; } try { if (Common.IsPosixEnvironment) { foreach (WeakReference <Process> childProcessReference in m_childProcesses) { Process childProcess; if (!childProcessReference.TryGetTarget(out childProcess)) { continue; } try { childProcess.Kill(); } catch (Exception ex) { TerminationException?.Invoke(this, new EventArgs <Exception>(ex)); } } } m_jobHandle?.Dispose(); m_jobHandle = null; } finally { m_disposed = true; GC.SuppressFinalize(this); } }
/// <summary> /// Creates a new <see cref="ChildProcessManager"/>. /// </summary> public ChildProcessManager() { if (Common.IsPosixEnvironment) { // On non-Windows operating systems we just track associated processes m_childProcesses = new List <WeakReference <Process> >(); } else { // Let safe handle manage terminations on Windows GC.SuppressFinalize(this); // On Windows we add child processes to a job object such that when the job // is terminated, so are the child processes. Since safe handle ensures proper // closing of job handle, child processes will be terminated even if parent // process is abnormally terminated m_jobHandle = new SafeJobHandle(CreateJobObject(IntPtr.Zero, null)); JOBOBJECT_BASIC_LIMIT_INFORMATION info = new JOBOBJECT_BASIC_LIMIT_INFORMATION { LimitFlags = 0x2000 }; JOBOBJECT_EXTENDED_LIMIT_INFORMATION extendedInfo = new JOBOBJECT_EXTENDED_LIMIT_INFORMATION { BasicLimitInformation = info }; int length = Marshal.SizeOf(typeof(JOBOBJECT_EXTENDED_LIMIT_INFORMATION)); IntPtr extendedInfoPtr = Marshal.AllocHGlobal(length); Marshal.StructureToPtr(extendedInfo, extendedInfoPtr, false); if (!SetInformationJobObject(m_jobHandle, JobObjectInfoType.ExtendedLimitInformation, extendedInfoPtr, (uint)length)) { throw new InvalidOperationException($"Unable to set information for ChildProcessManager job. Error: {Marshal.GetLastWin32Error()}"); } } }
public ChildProcessManager() { _handle = new SafeJobHandle(CreateJobObject(IntPtr.Zero, null)); var info = new JOBOBJECT_BASIC_LIMIT_INFORMATION { LimitFlags = 0x2000 }; var extendedInfo = new JOBOBJECT_EXTENDED_LIMIT_INFORMATION { BasicLimitInformation = info }; var length = Marshal.SizeOf(typeof(JOBOBJECT_EXTENDED_LIMIT_INFORMATION)); var extendedInfoPtr = Marshal.AllocHGlobal(length); Marshal.StructureToPtr(extendedInfo, extendedInfoPtr, false); if (!SetInformationJobObject(_handle, JobObjectInfoType.ExtendedLimitInformation, extendedInfoPtr, (uint)length)) { throw new InvalidOperationException($"Unable to set information. Error: {Marshal.GetLastWin32Error()}"); } }
internal static extern Boolean SetInformationJobObject(SafeJobHandle hJob, JobObjectInfoType infoType, IntPtr lpJobObjectInfo, UInt32 cbJobObjectInfoLength);
public static extern bool SetInformationJobObject(SafeJobHandle jobHandle, JobObjectInfoType infoType, [In] ref JOBOBJECT_EXTENDED_LIMIT_INFORMATION jobObjectInfo, uint jobObjectInfoLength);
internal static unsafe extern bool AssignProcessToJobObject(SafeJobHandle jobHandle, SafeProcessHandle procHandle);
internal static extern unsafe bool QueryInformationJobObject(SafeJobHandle handle, JobInformationClass informationClass, void *buffer, uint bufferLength, out uint returnLength);
internal static extern unsafe bool SetInformationJobObject(SafeJobHandle handle, JobInformationClass informationClass, void *buffer, uint bufferLength);
internal static extern unsafe bool TerminateJobObject(SafeJobHandle handle, UInt32 exitCode);
private static extern bool AssignProcessToJobObject(SafeJobHandle job, SafeProcessHandle process);
private NativeJob(SafeJobHandle handle) { this.handle = handle; }
private static extern bool SetInformationJobObject(SafeJobHandle hJob, JobObjectInfoType infoType, IntPtr lpJobObjectInfo, uint cbJobObjectInfoLength);
public static extern bool SetInformationJobObject(SafeJobHandle hJob, JobObjectInfoType infoType, ref JOBOBJECT_EXTENDED_LIMIT_INFORMATION lpJobObjectInfo, int cbJobObjectInfoLength);
private static extern bool SetInformationJobObject(SafeJobHandle jobHandle, JobObjectInfoType infoType, IntPtr lpJobObjectInfo, uint cbJobObjectInfoLength);
internal static extern unsafe bool IsProcessInJobNative(SafeProcessHandle processHandle, SafeJobHandle jobHandle, [MarshalAs(UnmanagedType.Bool)] out bool result);
public InformationObjectManager() { _handle = new SafeJobHandle(NativeMethods.CreateJobObject(IntPtr.Zero, null)); SetHandle(NativeMethods.JobObjectLimitFlags.JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE); }
internal static extern unsafe bool UserHandleGrantAccess(IntPtr userHandle, SafeJobHandle jobHandle, [MarshalAs(UnmanagedType.Bool)] bool grant);
private static extern bool AssignProcessToJobObject(SafeJobHandle jobHandle, SafeProcessHandle process);
internal static extern Boolean AssignProcessToJobObject(SafeJobHandle job, SafeProcessHandle process);
public static extern bool AssignProcessToJobObject(SafeJobHandle job, IntPtr process);
private JobObject(SafeJobHandle safeHandle) => SafeHandle = safeHandle;