Example #1
0
        /// <summary>
        ///     Determines if the given <paramref name="process"/> belongs to the specified
        ///     <paramref name="jobObjectHandle"/>.
        /// </summary>
        /// <param name="process">Process to check Job Object membership of.</param>
        /// <param name="jobObjectHandle">Job Object to check for membership.</param>
        /// <returns>
        ///     <c>true</c> if the given <paramref name="process"/> belongs to the specified
        ///     <paramref name="jobObjectHandle"/>; otherwise <c>false</c>.
        /// </returns>
        internal static bool IsProcessInJob(Process process, IntPtr jobObjectHandle)
        {
            var status = false;

            PInvokeUtils.Try(() => JobObjectAPI.IsProcessInJob(process.Handle, jobObjectHandle, out status));
            return(status);
        }
Example #2
0
 public void TestThrowsWin32Exception()
 {
     Assert.Throws <Win32Exception>(delegate
     {
         var result1 = PInvokeUtils.Try(() => JobObjectAPI.CloseHandle(IntPtr.Zero), result => result);
     });
 }
Example #3
0
        public void KillOnClose()
        {
            var type   = JobObjectInfoClass.ExtendedLimitInformation;
            var limit  = CreateKillOnCloseJobObjectInfo();
            var length = GetKillOnCloseJobObjectInfoLength();

            PInvokeUtils.Try(() => JobObjectAPI.SetInformationJobObject(_jobObjectHandle, type, ref limit, length));
        }
Example #4
0
        public void TestReturnsResult()
        {
            var jobObjectHandle = IntPtr.Zero;

            try
            {
                jobObjectHandle = PInvokeUtils.Try(() => JobObjectAPI.CreateJobObject(IntPtr.Zero, null), result => result != IntPtr.Zero);

                Assert.AreNotEqual(jobObjectHandle, IntPtr.Zero);
            }
            finally
            {
                if (jobObjectHandle != IntPtr.Zero)
                {
                    JobObjectAPI.CloseHandle(jobObjectHandle);
                }
            }
        }
Example #5
0
        private static Process CreateProcessInSeparateJob(ProcessStartInfo startInfo)
        {
            var securityAttributes = new SECURITY_ATTRIBUTES
            {
                nLength = Marshal.SizeOf(typeof(SECURITY_ATTRIBUTES)),
                lpSecurityDescriptor = IntPtr.Zero,
                bInheritHandle       = false
            };

            var          environment                 = IntPtr.Zero;
            const bool   inheritHandles              = false;
            const string currentDirectory            = null;
            const ProcessCreationFlags creationFlags = ProcessCreationFlags.CREATE_BREAKAWAY_FROM_JOB;

            var startupInfo = new STARTUPINFO {
                cb = Marshal.SizeOf(typeof(STARTUPINFO))
            };
            var processInformation = new PROCESS_INFORMATION();

            PInvokeUtils.Try(() => JobObjectAPI.CreateProcess(startInfo.FileName,
                                                              startInfo.Arguments,
                                                              ref securityAttributes,
                                                              ref securityAttributes,
                                                              inheritHandles,
                                                              creationFlags,
                                                              environment,
                                                              currentDirectory,
                                                              ref startupInfo,
                                                              out processInformation));

            try
            {
                return(Process.GetProcessById(processInformation.dwProcessId));
            }
            catch (Exception e)
            {
                Logger.Error("Unable to get child process by ID.  " +
                             "Are you running in Visual Studio with Program Compatibility Assistant (PCA) enabled?  " +
                             "See http://stackoverflow.com/a/4232259/3205 for more information.", e);
                return(null);
            }
        }
Example #6
0
        /// <summary>
        ///     Assigns the given process to this Job Object.
        /// </summary>
        /// <param name="process">Process to assign to this Job Object.</param>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="process"/> is <c>null</c>.</exception>
        /// <exception cref="InvalidOperationException">
        ///     Thrown if <paramref name="process"/> already belongs to a Job Object.
        /// </exception>
        /// <exception cref="Win32Exception">
        ///     Thrown if the operating system was unable to assign <paramref name="process"/> to the Job Object.
        /// </exception>
        public void Assign(Process process)
        {
            if (process == null)
            {
                throw new ArgumentNullException("process");
            }

            if (AlreadyAssigned(process))
            {
                return;
            }

            if (HasJobObject(process))
            {
                throw new InvalidOperationException(
                          "Requested process already belongs to another job group.  Check http://stackoverflow.com/a/4232259/3205 for help.");
            }

            PInvokeUtils.Try(() => JobObjectAPI.AssignProcessToJobObject(_jobObjectHandle, process.Handle));
        }
Example #7
0
        /// <summary>
        ///     Frees managed and unmanaged resources.
        /// </summary>
        /// <param name="freeManagedObjectsAlso">
        ///     Free managed resources.  Should only be set to <c>true</c> when called from <see href="Dispose"/>.
        /// </param>
        /// <exception cref="Win32Exception">
        ///     Thrown if the handle to the Job Object could not be released.
        /// </exception>
        /// <seealso href="http://stackoverflow.com/a/538238/467582"/>
        private void Dispose(bool freeManagedObjectsAlso)
        {
            // Free unmanaged resources
            // ...

            // Free managed resources too, but only if I'm being called from Dispose()
            // (If I'm being called from Finalize then the objects might not exist anymore)
            if (freeManagedObjectsAlso)
            {
                if (_disposed)
                {
                    return;
                }
                if (_jobObjectHandle == IntPtr.Zero)
                {
                    return;
                }

                _disposed = true;

                PInvokeUtils.Try(() => JobObjectAPI.CloseHandle(_jobObjectHandle));
            }
        }
Example #8
0
 /// <exception cref="Win32Exception">
 ///     Thrown if the operating system was unable to create a new Job Object.
 /// </exception>
 public JobObject()
 {
     _jobObjectHandle = PInvokeUtils.Try(() => JobObjectAPI.CreateJobObject(IntPtr.Zero, null));
 }