Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
55 lines (52 loc) · 1.81 KB
/
Copy pathProgram.cs
File metadata and controls
55 lines (52 loc) · 1.81 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
54
55
using System;
using JobManagement;
using System.Threading;
namespace EndOfProcessMemory
{
/// <summary>
/// Demonstrating the Process Memory Limit usage
/// </summary>
class Program
{
private static bool _isStop;
static void Main()
{
using (JobObject jo = new JobObject("ProcessMemoryLimitExample"))
{
jo.Limits.ProcessMemoryLimit = new IntPtr(1<<22);
jo.Events.OnProcessMemoryLimit += Events_OnProcessMemoryLimit;
try
{
System.Diagnostics.ProcessStartInfo si =
new System.Diagnostics.ProcessStartInfo("cmd.exe")
{
RedirectStandardInput = true,
UseShellExecute = false
};
System.Diagnostics.Process p = jo.CreateProcessMayBreakAway(si);
p.StandardInput.WriteLine(@"cd\");
while (!_isStop)
{
p.StandardInput.WriteLine("dir /s");
Thread.Sleep(1500);
}
}
// ReSharper disable once EmptyGeneralCatchClause
catch
{
}
}
}
/// <summary>
/// The event handler for the OnProcessMemoryLimit event
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
private static void Events_OnProcessMemoryLimit(object sender, ProcessMemoryLimitEventArgs args)
{
_isStop = true;
Thread.Sleep(500);
Console.WriteLine("Process has reached its memory limit");
}
}
}