예제 #1
0
 public void AssignProcess(SafeProcessHandle processHandlerocess)
 {
     if (!Kernel32.AssignProcessToJobObject(this, processHandlerocess))
     {
         throw new Win32Exception(Marshal.GetLastWin32Error());
     }
 }
 private void AddProcess(IntPtr processHandle)
 {
     if (Kernel32.AssignProcessToJobObject(jobHandle, processHandle))
     {
         return;
     }
     log.Error(new Win32Exception(Marshal.GetLastWin32Error()), "Failed to add process to job.");
 }
예제 #3
0
        public void AssignProcess(SafeProcessHandle handle)
        {
            var result = Kernel32.AssignProcessToJobObject(mHandle, handle);

            if (!result)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }
        }
예제 #4
0
 /// <summary>
 /// Add the process to the job.
 /// </summary>
 public bool AddProcess
 (
     IntPtr processHandle
 )
 {
     return(Kernel32.AssignProcessToJobObject
            (
                _handle,
                processHandle
            ));
 }
        private void AddProcess(IntPtr processHandle)
        {
            if (Kernel32.AssignProcessToJobObject(jobHandle, processHandle))
            {
                return;
            }
            var error = Marshal.GetLastWin32Error();

            if (!Kernel32.GetExitCodeProcess(processHandle, out var exitCode) || exitCode == ProcessExitCodes.STILL_ACTIVE)
            {
                throw new Win32Exception(error);
            }
        }
        public void Start(string port, int baudRate, uint chip)
        {
            Stop();
            var arg = $"-serial {port} -sercfg {baudRate},8,1,N,N";

            if (chip == 3)
            {
                arg += " -mem";
            }

            _process = Process.Start(new ProcessStartInfo(_plinkFile, arg)
            {
                UseShellExecute = false
            });

            System.Threading.Thread.Sleep(100);
            User32.SetWindowText(_process.MainWindowHandle, "K-Flash Terminal");
            Kernel32.AssignProcessToJobObject(_job, new Kernel32.SafeObjectHandle(_process.Handle, false));
        }
예제 #7
0
        public bool AddProcess(IntPtr handle)
        {
            var processHandle = new Kernel32.SafeObjectHandle(handle, false);

            return(Kernel32.AssignProcessToJobObject(_handle, processHandle));
        }
예제 #8
0
        static int Main(string[] args)
        {
            try {
                if (args.Length < 1)
                {
                    Usage();
                    return(1);
                }

                string program   = args[0];
                string arguments = String.Join(" ", args, 1, args.Length - 1);

                IntPtr job_handle = Kernel32.CreateJobObject();
                if (job_handle == IntPtr.Zero)
                {
                    WriteLine("WARNING: Failed to create job object; cannot gather CPU usage info.");
                }

                using (Process process = new Process()) {
                    process.StartInfo.UseShellExecute = false;
                    process.StartInfo.Arguments       = arguments;
                    process.StartInfo.FileName        = program;

                    DateTime timeStarted = DateTime.Now;

                    process.Start();

                    if (job_handle != IntPtr.Zero)
                    {
                        if (!Kernel32.AssignProcessToJobObject(job_handle, process.Handle))
                        {
                            WriteLine("WARNING: Failed to assign child process to job.");
                            WriteLine("The process may have already terminated.");
                        }
                    }

                    process.WaitForExit();

                    DateTime timeEnded = DateTime.Now;

                    TimeSpan elapsed = timeEnded - timeStarted;

                    const string times_format = " {0,-32} : {1}";

                    if (job_handle != IntPtr.Zero)
                    {
                        try {
                            JOBOBJECT_BASIC_ACCOUNTING_INFORMATION job_info;
                            job_info = Kernel32.QueryJobObjectBasicAccountingInformation(job_handle);

                            TimeSpan total_job_time = job_info.TotalUserTime + job_info.TotalKernelTime;

                            WriteLine(
                                times_format,
                                "CPU time consumed by jobs",
                                total_job_time);

                            WriteLine(
                                times_format,
                                "    in user mode",
                                job_info.TotalUserTime);

                            WriteLine(
                                times_format,
                                "    in kernel mode",
                                job_info.TotalKernelTime);

                            WriteLine(
                                times_format,
                                "Elapsed time (wall time)",
                                elapsed);

                            WriteLine("");
                            if (elapsed.TotalMilliseconds > 0)
                            {
                                double elapsed_ms         = elapsed.TotalMilliseconds;
                                double total_job_ms       = total_job_time.TotalMilliseconds;
                                double throughput_ratio   = total_job_ms / elapsed_ms;
                                double throughput_percent = Math.Round(throughput_ratio * 100.0);
                                WriteLine("      Parallel throughput ratio: {0}%", throughput_percent);
                            }
                            else
                            {
                                WriteLine("      Too little time elapsed to compute ratio.");
                            }
                        }
                        catch (Exception ex) {
                            WriteLine("FAILED to query job info!");
                            ShowException(ex);
                        }
                    }
                    else
                    {
                        WriteLine(" Child process times not available.");
                    }

                    return(process.ExitCode);
                }
            }
            catch (Exception ex) {
                ShowException(ex);
                return(1);
            }
        }