Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
53 lines (48 loc) · 1.74 KB
/
Copy pathProgram.cs
File metadata and controls
53 lines (48 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using System;
using JobManagement;
using System.Threading;
namespace NumOfMaxProcessesReached
{
/// <summary>
/// Demonstrate the number of active processes limit usage
/// </summary>
class Program
{
static void Main()
{
try
{
using (JobObject jo = new JobObject("ProcessLimitExample"))
{
jo.Limits.ActiveProcessLimit = 4;
jo.Events.OnActiveProcessLimit += Events_OnActiveProcessLimit;
while (true)
{
System.Diagnostics.ProcessStartInfo si =
new System.Diagnostics.ProcessStartInfo("cmd.exe")
{
RedirectStandardInput = true,
UseShellExecute = false
};
System.Diagnostics.Process p = jo.CreateProcessMayBreakAway(si);
Console.WriteLine($"\nCreated process with id:{p.Id} out of maximum {jo.Limits.ActiveProcessLimit} processes");
Thread.Sleep(1500);
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
/// <summary>
/// The event handler for the OnActiveProcessLimit event
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
static void Events_OnActiveProcessLimit(object sender, ActiveProcessLimitEventArgs args)
{
Console.WriteLine("\nLimit of active processes reached");
}
}
}