@@ -21,23 +21,18 @@ public static class ChildProcessTracker
21
21
/// <param name="process"></param>
22
22
public static void AddProcess ( Process ? process )
23
23
{
24
- if ( Environment . OSVersion . Platform != PlatformID . Win32NT )
25
- {
26
- //Only supported on windows
27
- return ;
28
- }
29
-
30
24
if ( process == null )
31
25
return ;
32
26
33
- if ( s_jobHandle != IntPtr . Zero )
27
+ if ( SJobHandle != IntPtr . Zero )
34
28
{
35
- bool success = AssignProcessToJobObject ( s_jobHandle , process . Handle ) ;
29
+ bool success = AssignProcessToJobObject ( SJobHandle , process . Handle ) ;
36
30
if ( ! success && ! process . HasExited )
37
31
throw new System . ComponentModel . Win32Exception ( ) ;
38
32
}
39
33
}
40
-
34
+ #pragma warning disable S3963
35
+ #pragma warning disable S3877
41
36
static ChildProcessTracker ( )
42
37
{
43
38
// This feature requires Windows 8 or later. To support Windows 7 requires
@@ -51,26 +46,29 @@ static ChildProcessTracker()
51
46
// The job name is optional (and can be null) but it helps with diagnostics.
52
47
// If it's not null, it has to be unique. Use SysInternals' Handle command-line
53
48
// utility: handle -a ChildProcessTracker
54
- string jobName = "ChildProcessTracker" + Process . GetCurrentProcess ( ) . Id ;
55
- s_jobHandle = CreateJobObject ( IntPtr . Zero , jobName ) ;
56
-
57
- var info = new JOBOBJECT_BASIC_LIMIT_INFORMATION ( ) ;
49
+ string jobName = "ChildProcessTracker" + Environment . ProcessId ;
50
+ SJobHandle = CreateJobObject ( IntPtr . Zero , jobName ) ;
58
51
59
- // This is the key flag. When our process is killed, Windows will automatically
60
- // close the job handle, and when that happens, we want the child processes to
61
- // be killed, too.
62
- info . LimitFlags = JOBOBJECTLIMIT . JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE ;
52
+ var info = new JobObjectBasicLimitInformation
53
+ {
54
+ // be killed, too.
55
+ // close the job handle, and when that happens, we want the child processes to
56
+ // This is the key flag. When our process is killed, Windows will automatically
57
+ LimitFlags = JobObjectLimits . JobObjectLimitKillOnJobClose
58
+ } ;
63
59
64
- var extendedInfo = new JOBOBJECT_EXTENDED_LIMIT_INFORMATION ( ) ;
65
- extendedInfo . BasicLimitInformation = info ;
60
+ var extendedInfo = new JobObjectExtendedLimitInformation
61
+ {
62
+ BasicLimitInformation = info
63
+ } ;
66
64
67
- int length = Marshal . SizeOf ( typeof ( JOBOBJECT_EXTENDED_LIMIT_INFORMATION ) ) ;
65
+ int length = Marshal . SizeOf ( typeof ( JobObjectExtendedLimitInformation ) ) ;
68
66
IntPtr extendedInfoPtr = Marshal . AllocHGlobal ( length ) ;
69
67
try
70
68
{
71
69
Marshal . StructureToPtr ( extendedInfo , extendedInfoPtr , false ) ;
72
70
73
- if ( ! SetInformationJobObject ( s_jobHandle , JobObjectInfoType . ExtendedLimitInformation ,
71
+ if ( ! SetInformationJobObject ( SJobHandle , JobObjectInfoType . ExtendedLimitInformation ,
74
72
extendedInfoPtr , ( uint ) length ) )
75
73
{
76
74
throw new System . ComponentModel . Win32Exception ( ) ;
@@ -81,40 +79,41 @@ static ChildProcessTracker()
81
79
Marshal . FreeHGlobal ( extendedInfoPtr ) ;
82
80
}
83
81
}
82
+ #pragma warning restore S3877
83
+ #pragma warning restore S3963
84
84
85
85
[ DllImport ( "kernel32.dll" , CharSet = CharSet . Unicode ) ]
86
86
static extern IntPtr CreateJobObject ( IntPtr lpJobAttributes , string name ) ;
87
87
88
88
[ DllImport ( "kernel32.dll" ) ]
89
- static extern bool SetInformationJobObject ( IntPtr job , JobObjectInfoType infoType ,
90
- IntPtr lpJobObjectInfo , uint cbJobObjectInfoLength ) ;
89
+ static extern bool SetInformationJobObject ( IntPtr job , JobObjectInfoType infoType , IntPtr lpJobObjectInfo , uint cbJobObjectInfoLength ) ;
91
90
92
91
[ DllImport ( "kernel32.dll" , SetLastError = true ) ]
93
92
static extern bool AssignProcessToJobObject ( IntPtr job , IntPtr process ) ;
94
93
95
94
// Windows will automatically close any open job handles when our process terminates.
96
95
// This can be verified by using SysInternals' Handle utility. When the job handle
97
96
// is closed, the child processes will be killed.
98
- private static readonly IntPtr s_jobHandle ;
97
+ private static readonly IntPtr SJobHandle ;
99
98
}
100
99
101
100
public enum JobObjectInfoType
102
101
{
103
102
AssociateCompletionPortInformation = 7 ,
104
103
BasicLimitInformation = 2 ,
105
- BasicUIRestrictions = 4 ,
104
+ BasicUiRestrictions = 4 ,
106
105
EndOfJobTimeInformation = 6 ,
107
106
ExtendedLimitInformation = 9 ,
108
107
SecurityLimitInformation = 5 ,
109
108
GroupInformation = 11
110
109
}
111
110
112
111
[ StructLayout ( LayoutKind . Sequential ) ]
113
- public struct JOBOBJECT_BASIC_LIMIT_INFORMATION
112
+ public struct JobObjectBasicLimitInformation
114
113
{
115
114
public Int64 PerProcessUserTimeLimit ;
116
115
public Int64 PerJobUserTimeLimit ;
117
- public JOBOBJECTLIMIT LimitFlags ;
116
+ public JobObjectLimits LimitFlags ;
118
117
public UIntPtr MinimumWorkingSetSize ;
119
118
public UIntPtr MaximumWorkingSetSize ;
120
119
public UInt32 ActiveProcessLimit ;
@@ -124,13 +123,13 @@ public struct JOBOBJECT_BASIC_LIMIT_INFORMATION
124
123
}
125
124
126
125
[ Flags ]
127
- public enum JOBOBJECTLIMIT : uint
126
+ public enum JobObjectLimits : uint
128
127
{
129
- JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE = 0x2000
128
+ JobObjectLimitKillOnJobClose = 0x2000
130
129
}
131
130
132
131
[ StructLayout ( LayoutKind . Sequential ) ]
133
- public struct IO_COUNTERS
132
+ public struct IoCounters
134
133
{
135
134
public UInt64 ReadOperationCount ;
136
135
public UInt64 WriteOperationCount ;
@@ -141,10 +140,10 @@ public struct IO_COUNTERS
141
140
}
142
141
143
142
[ StructLayout ( LayoutKind . Sequential ) ]
144
- public struct JOBOBJECT_EXTENDED_LIMIT_INFORMATION
143
+ public struct JobObjectExtendedLimitInformation
145
144
{
146
- public JOBOBJECT_BASIC_LIMIT_INFORMATION BasicLimitInformation ;
147
- public IO_COUNTERS IoInfo ;
145
+ public JobObjectBasicLimitInformation BasicLimitInformation ;
146
+ public IoCounters IoInfo ;
148
147
public UIntPtr ProcessMemoryLimit ;
149
148
public UIntPtr JobMemoryLimit ;
150
149
public UIntPtr PeakProcessMemoryUsed ;
0 commit comments