Пример #1
0
        public void KillOnJobClose_ShouldKillProcessOnClose()
        {
            using (var job = new JobObject())
            {
                job.SetLimits(new JobObjectLimits()
                {
                    Flags = JobObjectLimitFlags.DieOnUnhandledException | JobObjectLimitFlags.KillOnJobClose,
                });

                var psi = new ProcessStartInfo
                {
                    FileName        = "ping",
                    Arguments       = "127.0.0.1 -n 100",
                    UseShellExecute = true,
                    CreateNoWindow  = true,
                };

                using (var process = Process.Start(psi))
                {
                    Assert.False(process.WaitForExit(500)); // Ensure process is started

                    job.AssignProcess(process);
                    job.Close();

                    process.WaitForExit();
                }
            }
        }
Пример #2
0
        public async Task RunAsync()
        {
            using (var jobObject = new JobObject())
            {
                // setup some limits, we do this before starting any processes, but it's not
                // required..

                var jobLimits = new JobLimits();
                jobLimits.CpuRate         = new CpuRateLimit(10.0m, false);
                jobLimits.Options         = JobOptions.TerminateProcessesWhenJobClosed;
                jobLimits.ActiveProcesses = 3;
                jobObject.SetLimits(jobLimits);

                // setup a few event handlers..
                jobObject.ProcessAdded         += (s, e) => Console.WriteLine($"Process {e.ID} added.");
                jobObject.ProcessExited        += (s, e) => Console.WriteLine($"Process {e.ID} exited.");
                jobObject.ProcessLimitExceeded += (s, e) => Console.WriteLine("Process limit exceeded.");
                jobObject.CpuRateLimitExceeded += (s, e) => Console.WriteLine("CPU rate limit exceeded.");

                // configure CPU rate notification..
                var jobNotifications = new JobNotifications();
                jobNotifications.CpuRate = new RateControl(RateControlInterval.Short, RateControlTolerance.Low);

                // start some ping processes...
                var cpiPing = new CreateProcessInfo
                {
                    FileName      = "ping.exe",
                    ArgumentsList = { "8.8.8.8", "-t" }
                };

                // create ping processes, directly through the JobObject, this will create the process
                // and associate the process with the JobObject ..
                for (int iIndex = 0; iIndex < 10; iIndex++)
                {
                    try
                    {
                        using (var process = jobObject.CreateProcess(cpiPing))
                            Console.WriteLine($"Created Process {process.Id}");
                    }
                    catch (System.ComponentModel.Win32Exception ex) when(ex.NativeErrorCode == ERROR_NOT_ENOUGH_QUOTA)
                    {
                        // ERROR_NOT_ENOUGH_QUOTA happens if the process cannot be assigned to the job object
                        // because of the active process limit..

                        Console.WriteLine("JobObject.CreateProcess failed, due to process limit.");
                    }
                }

                Console.WriteLine("[enter] to terminate active processes.");
                Console.ReadLine();
                jobObject.Kill();

                // wait for JobObject to become idle..
                await jobObject.Idle;
            }
        }