Пример #1
0
 public virtual void Dispose()
 {
     if (handle != null)
     {
         handle.Dispose();
         handle = null;
     }
 }
Пример #2
0
        public virtual void Dispose()
        {
            if (handle != null)
            {
                handle.Dispose();
                handle = null;
            }

            if (completionPort != null)
            {
                completionPort.Dispose();
                completionPort = null;
            }
        }
Пример #3
0
        public JobObject(string name, bool openExisting, bool terminateOnLastHandleClose = true)
        {
            if (openExisting)
            {
                handle = new SafeJobObjectHandle(NativeMethods.OpenJobObject(NativeMethods.JobObjectAccessRights.AllAccess, false, name));
            }
            else
            {
                var token = NativeMethods.CreateJobObject(IntPtr.Zero, name);
                if (token == IntPtr.Zero)
                {
                    throw new Win32Exception(Marshal.GetLastWin32Error());
                }
                handle = new SafeJobObjectHandle(token);
                SetJobLimits(NativeMethods.JobObjectLimit.KillOnJobClose);
            }

            if (handle.IsInvalid)
            {
                throw new Exception("Unable to create job object.");
            }
        }
Пример #4
0
        public JobObject(string name, bool openExisting, bool terminateOnLastHandleClose = true)
        {
            if (openExisting)
            {
                handle = new SafeJobObjectHandle(NativeMethods.OpenJobObject(NativeMethods.JobObjectAccessRights.AllAccess, false, name));
            }
            else
            {
                var token = NativeMethods.CreateJobObject(IntPtr.Zero, name);
                if (token == IntPtr.Zero)
                {
                    throw new Win32Exception(Marshal.GetLastWin32Error());
                }
                handle = new SafeJobObjectHandle(token);
                SetJobLimits(NativeMethods.JobObjectLimit.KillOnJobClose);
            }

            if (handle.IsInvalid)
            {
                throw new Exception("Unable to create job object.");
            }
        }
Пример #5
0
 public virtual void Dispose()
 {
     if (handle != null)
     {
         handle.Dispose();
         handle = null;
     }
 }
Пример #6
0
        static int[] GetJobObjectProcessIds(SafeJobObjectHandle handle)
        {
            const int JobCountIncrement = 5;

            int numberOfAssignedProcessesOffset = Marshal.OffsetOf(typeof(NativeMethods.JobObjectBasicProcessIdList), "NumberOfAssignedProcesses").ToInt32();
            int numberOfProcessIdsInListOffset = Marshal.OffsetOf(typeof(NativeMethods.JobObjectBasicProcessIdList), "NumberOfProcessIdsInList").ToInt32();
            int firstProcessIdOffset = Marshal.OffsetOf(typeof(NativeMethods.JobObjectBasicProcessIdList), "FirstProcessId").ToInt32();

            int numberOfProcessesInJob = JobCountIncrement;
            do
            {
                int infoSize = firstProcessIdOffset + (IntPtr.Size * numberOfProcessesInJob);
                IntPtr infoPtr = IntPtr.Zero;
                try
                {
                    infoPtr = Marshal.AllocHGlobal(infoSize);
                    NativeMethods.FillMemory(infoPtr, (IntPtr)infoSize, 0);

                    Marshal.WriteInt32(infoPtr, numberOfAssignedProcessesOffset, numberOfProcessesInJob);
                    Marshal.WriteInt32(infoPtr, numberOfProcessIdsInListOffset, 0);

                    if (!NativeMethods.QueryInformationJobObject(
                        handle,
                        NativeMethods.JobObjectInfoClass.JobObjectBasicProcessIdList,
                        infoPtr,
                        infoSize,
                        IntPtr.Zero))
                    {
                        var error = Marshal.GetLastWin32Error();
                        if (error == NativeMethods.Constants.ERROR_MORE_DATA)
                        {
                            numberOfProcessesInJob += JobCountIncrement;
                            continue;
                        }

                        throw new Win32Exception(error);
                    }

                    int count = Marshal.ReadInt32(infoPtr, numberOfProcessIdsInListOffset);
                    if (count == 0)
                        return new int[0];

                    IntPtr[] ids = new IntPtr[count];

                    Marshal.Copy(infoPtr + firstProcessIdOffset, ids, 0, count);

                    return ids.Select(id => id.ToInt32()).ToArray();
                }
                finally
                {
                    if (infoPtr != IntPtr.Zero)
                        Marshal.FreeHGlobal(infoPtr);
                }

            } while (true);
        }
Пример #7
0
        static NativeMethods.JobObjectBasicAccountingInformation GetJobObjectBasicAccountingInformation(SafeJobObjectHandle handle)
        {
            int infoSize = Marshal.SizeOf(typeof(NativeMethods.JobObjectBasicAccountingInformation));
            IntPtr infoPtr = IntPtr.Zero;
            try
            {
                infoPtr = Marshal.AllocHGlobal(infoSize);

                if (!NativeMethods.QueryInformationJobObject(
                    handle,
                    NativeMethods.JobObjectInfoClass.JobObjectBasicAccountingInformation,
                    infoPtr,
                    infoSize,
                    IntPtr.Zero))
                {
                    var error = Marshal.GetLastWin32Error();
                    if (error != NativeMethods.Constants.ERROR_MORE_DATA)
                        throw new Win32Exception(error);
                }

                return (NativeMethods.JobObjectBasicAccountingInformation)Marshal.PtrToStructure(infoPtr, typeof(NativeMethods.JobObjectBasicAccountingInformation));
            }
            finally
            {
                if (infoPtr != IntPtr.Zero)
                    Marshal.FreeHGlobal(infoPtr);
            }
        }
Пример #8
0
 public JobObjectWaitHandle(SafeJobObjectHandle jobObject)
 {
     SafeWaitHandle = new SafeWaitHandle(jobObject.DangerousGetHandle(), false);
 }
 public JobObjectWaitHandle(SafeJobObjectHandle jobObject)
 {
     SafeWaitHandle = new SafeWaitHandle(jobObject.DangerousGetHandle(), false);
 }
Пример #10
0
        static int[] GetJobObjectProcessIds(SafeJobObjectHandle handle)
        {
            const int JobCountIncrement = 5;

            int numberOfAssignedProcessesOffset = Marshal.OffsetOf(typeof(NativeMethods.JobObjectBasicProcessIdList), "NumberOfAssignedProcesses").ToInt32();
            int numberOfProcessIdsInListOffset  = Marshal.OffsetOf(typeof(NativeMethods.JobObjectBasicProcessIdList), "NumberOfProcessIdsInList").ToInt32();
            int firstProcessIdOffset            = Marshal.OffsetOf(typeof(NativeMethods.JobObjectBasicProcessIdList), "FirstProcessId").ToInt32();

            int numberOfProcessesInJob = JobCountIncrement;

            do
            {
                int    infoSize = firstProcessIdOffset + (IntPtr.Size * numberOfProcessesInJob);
                IntPtr infoPtr  = IntPtr.Zero;
                try
                {
                    infoPtr = Marshal.AllocHGlobal(infoSize);
                    NativeMethods.FillMemory(infoPtr, (IntPtr)infoSize, 0);

                    Marshal.WriteInt32(infoPtr, numberOfAssignedProcessesOffset, numberOfProcessesInJob);
                    Marshal.WriteInt32(infoPtr, numberOfProcessIdsInListOffset, 0);


                    if (!NativeMethods.QueryInformationJobObject(
                            handle,
                            NativeMethods.JobObjectInfoClass.JobObjectBasicProcessIdList,
                            infoPtr,
                            infoSize,
                            IntPtr.Zero))
                    {
                        var error = Marshal.GetLastWin32Error();
                        if (error == NativeMethods.Constants.ERROR_MORE_DATA)
                        {
                            numberOfProcessesInJob += JobCountIncrement;
                            continue;
                        }

                        throw new Win32Exception(error);
                    }

                    int count = Marshal.ReadInt32(infoPtr, numberOfProcessIdsInListOffset);
                    if (count == 0)
                    {
                        return(new int[0]);
                    }

                    IntPtr[] ids = new IntPtr[count];

                    Marshal.Copy(infoPtr + firstProcessIdOffset, ids, 0, count);

                    return(ids.Select(id => id.ToInt32()).ToArray());
                }
                finally
                {
                    if (infoPtr != IntPtr.Zero)
                    {
                        Marshal.FreeHGlobal(infoPtr);
                    }
                }
            } while (true);
        }
Пример #11
0
        static NativeMethods.JobObjectBasicAccountingInformation GetJobObjectBasicAccountingInformation(SafeJobObjectHandle handle)
        {
            int    infoSize = Marshal.SizeOf(typeof(NativeMethods.JobObjectBasicAccountingInformation));
            IntPtr infoPtr  = IntPtr.Zero;

            try
            {
                infoPtr = Marshal.AllocHGlobal(infoSize);

                if (!NativeMethods.QueryInformationJobObject(
                        handle,
                        NativeMethods.JobObjectInfoClass.JobObjectBasicAccountingInformation,
                        infoPtr,
                        infoSize,
                        IntPtr.Zero))
                {
                    var error = Marshal.GetLastWin32Error();
                    if (error != NativeMethods.Constants.ERROR_MORE_DATA)
                    {
                        throw new Win32Exception(error);
                    }
                }

                return((NativeMethods.JobObjectBasicAccountingInformation)Marshal.PtrToStructure(infoPtr, typeof(NativeMethods.JobObjectBasicAccountingInformation)));
            }
            finally
            {
                if (infoPtr != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(infoPtr);
                }
            }
        }