Beispiel #1
0
        public async Task RunAsync()
        {
            // setup a soft CPU rate limit of 10% and notification for when it is
            // exceeded..

            var jobLimits        = new JobLimits();
            var jobNotifications = new JobNotifications();

            jobLimits.CpuRate        = new CpuRateLimit(10.0m, false);
            jobNotifications.CpuRate = new RateControl(RateControlInterval.Short, RateControlTolerance.Low);

            using (var jobObject = new JobObject(jobLimits, jobNotifications))
            {
                // assume our working directory is the output path for the project, and that we're in
                // debug configuration..

                jobObject.CpuRateLimitExceeded += (s, e) =>
                {
                    Console.WriteLine("CPU rate exceeded");
                };

                using (var process = jobObject.CreateProcess(new CreateProcessInfo
                {
                    FileName = @"..\..\..\BusyApp\bin\Debug\BusyApp.exe"
                }))
                {
                    await process.Exited;
                }
            }
        }
        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;
            }
        }