public static ExtendedLimitInformation GetExtendedLimitInformation(int maximumTime, int maximumMemory) { var info = new BasicLimitInformation { LimitFlags = (int)(LimitFlags.JOB_OBJECT_LIMIT_JOB_MEMORY //// The following two flags are causing the process to have unexpected behavior //// | LimitFlags.JOB_OBJECT_LIMIT_JOB_TIME //// | LimitFlags.JOB_OBJECT_LIMIT_PROCESS_TIME | LimitFlags.JOB_OBJECT_LIMIT_ACTIVE_PROCESS | LimitFlags.JOB_OBJECT_LIMIT_DIE_ON_UNHANDLED_EXCEPTION | LimitFlags.JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE), PerJobUserTimeLimit = maximumTime, // TODO: Remove or rework PerProcessUserTimeLimit = maximumTime, ActiveProcessLimit = 1, }; var extendedInfo = new ExtendedLimitInformation { BasicLimitInformation = info, JobMemoryLimit = (UIntPtr)maximumMemory, IoInfo = { ReadTransferCount = 0, ReadOperationCount = 0, WriteOperationCount = 0, WriteTransferCount = 0 } }; return(extendedInfo); }
public void SetExtendedLimitInformation(ExtendedLimitInformation extendedInfo) { var length = Marshal.SizeOf(typeof(ExtendedLimitInformation)); var extendedInfoPointer = Marshal.AllocHGlobal(length); Marshal.StructureToPtr(extendedInfo, extendedInfoPointer, false); if (!NativeMethods.SetInformationJobObject(this.handle, InfoClass.ExtendedLimitInformation, extendedInfoPointer, (uint)length)) { throw new Win32Exception(); } }