Beispiel #1
0
        ManagedProcess(ProcessStartInfo startInfo, int timeoutMs, bool showWindow = false)
        {
            _process = new Process {
                StartInfo = startInfo
            };
            _timeoutMs = timeoutMs;

            if (showWindow)
            {
                // When launching the process, show the window. Don't redirect standard output so
                // it can appear in the console window if applicable, but still redirect standard
                // error so errors are logged.
                _process.StartInfo.RedirectStandardInput  = false;
                _process.StartInfo.RedirectStandardOutput = false;
                _process.StartInfo.RedirectStandardError  = true;
                _process.StartInfo.UseShellExecute        = false;
                _process.StartInfo.CreateNoWindow         = false;
            }
            else
            {
                _process.StartInfo.RedirectStandardInput  = true;
                _process.StartInfo.RedirectStandardOutput = true;
                _process.StartInfo.RedirectStandardError  = true;
                _process.StartInfo.UseShellExecute        = false;
                _process.StartInfo.CreateNoWindow         = true;
            }

            _process.OutputDataReceived += OutputHandler;
            _process.ErrorDataReceived  += ErrorHandler;

            _process.EnableRaisingEvents = true;
            _process.Exited += ExitHandler;

            _handle = CreateJobObject(IntPtr.Zero, null);
            var info = new JobObjectBasicLimitInformation
            {
                LimitFlags = 0x2000
            };

            var extendedInfo = new JobobjectExtendedLimitInformation
            {
                BasicLimitInformation = info
            };

            int    length          = Marshal.SizeOf(typeof(JobobjectExtendedLimitInformation));
            IntPtr extendedInfoPtr = Marshal.AllocHGlobal(length);

            Marshal.StructureToPtr(extendedInfo, extendedInfoPtr, false);

            if (!SetInformationJobObject(_handle, JobObjectInfoType.ExtendedLimitInformation,
                                         extendedInfoPtr, (uint)length))
            {
                throw new Exception(
                          ErrorStrings.FailedToSetJobLimitInfo(Marshal.GetLastWin32Error()));
            }
        }
Beispiel #2
0
        public unsafe void SetBasicLimit(JobObjectBasicLimitInformation limit)
        {
            var result = Kernel32.SetInformationJobObject(
                mHandle,
                JobObjectInfoClass.BasicLimitInformation,
                &limit,
                sizeof(JobObjectBasicLimitInformation)
                );

            if (!result)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="DatasetTrackingJob"/> class.
        /// </summary>
        public DatasetTrackingJob()
        {
            m_Handle = CreateJobObject(IntPtr.Zero, "Apollo");

            if (m_Handle == IntPtr.Zero)
            {
                var error = Marshal.GetLastWin32Error();
                throw new UnableToCreateJobException(
                          string.Format(
                              CultureInfo.InvariantCulture,
                              "Unable to create job object.  Error: {0}",
                              error));
            }

            var info = new JobObjectBasicLimitInformation
            {
                LimitFlags = JobObjectLimitKillOnJobClose
            };

            var extendedInfo = new JobObjectExtendedLimitInformation
            {
                BasicLimitInformation = info
            };

            int length          = Marshal.SizeOf(typeof(JobObjectExtendedLimitInformation));
            var extendedInfoPtr = Marshal.AllocHGlobal(length);

            Marshal.StructureToPtr(extendedInfo, extendedInfoPtr, false);

            if (!SetInformationJobObject(
                    m_Handle,
                    JobObjectInfoType.ExtendedLimitInformation,
                    extendedInfoPtr,
                    (uint)length))
            {
                var error = Marshal.GetLastWin32Error();
                throw new UnableToSetJobException(
                          string.Format(
                              CultureInfo.InvariantCulture,
                              "Unable to set information.  Error: {0}",
                              error));
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="DatasetTrackingJob"/> class.
        /// </summary>
        public DatasetTrackingJob()
        {
            m_Handle = CreateJobObject(IntPtr.Zero, "Apollo");
            if (m_Handle == IntPtr.Zero)
            {
                var error = Marshal.GetLastWin32Error();
                throw new UnableToCreateJobException(
                    string.Format(
                        CultureInfo.InvariantCulture,
                        "Unable to create job object.  Error: {0}",
                        error));
            }

            var info = new JobObjectBasicLimitInformation
                {
                    LimitFlags = JobObjectLimitKillOnJobClose
                };

            var extendedInfo = new JobObjectExtendedLimitInformation
                {
                    BasicLimitInformation = info
                };

            int length = Marshal.SizeOf(typeof(JobObjectExtendedLimitInformation));
            var extendedInfoPtr = Marshal.AllocHGlobal(length);
            Marshal.StructureToPtr(extendedInfo, extendedInfoPtr, false);

            if (!SetInformationJobObject(
                m_Handle,
                JobObjectInfoType.ExtendedLimitInformation,
                extendedInfoPtr,
                (uint)length))
            {
                var error = Marshal.GetLastWin32Error();
                throw new UnableToSetJobException(
                    string.Format(
                        CultureInfo.InvariantCulture,
                        "Unable to set information.  Error: {0}",
                        error));
            }
        }