-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathProgram.cs
More file actions
81 lines (72 loc) · 2.92 KB
/
Program.cs
File metadata and controls
81 lines (72 loc) · 2.92 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
using JobManagement;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace NestedJobs
{
class Program
{
static readonly System.Threading.ManualResetEvent FinishEvent = new System.Threading.ManualResetEvent(false);
static bool nested;
static void Main(string[] args)
{
nested = args.Length > 0;
try
{
using (JobObject jo = new JobObject(nested ? "NestedJobExampleChild" : "NestedJobExampleParent"))
{
jo.Events.OnEndOfProcessTime += Events_OnEndOfProcessTime;
Process childProcess;
if (nested)
{
Console.WriteLine("Nested Job");
jo.Limits.PerProcessUserTimeLimit = TimeSpan.FromMilliseconds(1000);
jo.Limits.IsKillOnJobHandleClose = true;
System.Diagnostics.ProcessStartInfo si =
new System.Diagnostics.ProcessStartInfo("cmd.exe")
{
RedirectStandardInput = true,
UseShellExecute = false
};
childProcess = jo.CreateProcessMayBreakAway(si);
childProcess.StandardInput.WriteLine("@for /L %n in (1,1,1000000) do @echo %n");
FinishEvent.WaitOne();
}
else
{
Console.WriteLine("Parent Job");
jo.Limits.PerProcessUserTimeLimit = TimeSpan.FromMilliseconds(50);
jo.Limits.IsKillOnJobHandleClose = true;
System.Diagnostics.ProcessStartInfo si =
new System.Diagnostics.ProcessStartInfo("NestedJobs.exe")
{
Arguments = "nested",
UseShellExecute = false,
};
childProcess = jo.CreateProcessMayBreakAway(si);
FinishEvent.WaitOne();
Console.ReadKey();
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
/// <summary>
/// The event handler for the OnEndOfProcessTime event
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
static void Events_OnEndOfProcessTime(object sender, EndOfProcessTimeEventArgs args)
{
Console.WriteLine("Process {0} has reached its time limit (in {1} job)", args.Win32Name,nested?"child":"parent");
FinishEvent.Set();
}
}
}