DreamineVMS 1.0.0.0
Windows PC의 RTSP·USB 카메라 영상을 HLS로 변환해 원격 CCTV Viewer에 전달하는 데스크톱 에이전트입니다.
로딩중...
검색중...
일치하는것 없음
ChildProcessJob.cs
이 파일의 문서화 페이지로 가기
1using System;
2using System.Diagnostics;
3using System.Runtime.InteropServices;
4using System.Runtime.Versioning;
5
7
24[SupportedOSPlatform("windows")]
25public sealed class ChildProcessJob : IDisposable
26{
35 private IntPtr _handle;
44 private bool _disposed;
45
55 {
56 _handle = CreateJobObject(IntPtr.Zero, null);
57 if (_handle == IntPtr.Zero)
58 {
59 // Job Object를 만들 수 없는 환경(권한 등)에서는 핸들이 0으로 남고,
60 // AddProcess는 무시됩니다. 이 경우 기존 동작(개별 Kill)으로 폴백됩니다.
61 return;
62 }
63
64 JOBOBJECT_BASIC_LIMIT_INFORMATION basicLimit = new()
65 {
67 };
68
69 JOBOBJECT_EXTENDED_LIMIT_INFORMATION extendedLimit = new()
70 {
71 BasicLimitInformation = basicLimit
72 };
73
74 int length = Marshal.SizeOf<JOBOBJECT_EXTENDED_LIMIT_INFORMATION>();
75 IntPtr extendedPtr = Marshal.AllocHGlobal(length);
76 try
77 {
78 Marshal.StructureToPtr(extendedLimit, extendedPtr, false);
79
81 _handle,
82 JobObjectInfoType.ExtendedLimitInformation,
83 extendedPtr,
84 (uint)length))
85 {
87 _handle = IntPtr.Zero;
88 }
89 }
90 finally
91 {
92 Marshal.FreeHGlobal(extendedPtr);
93 }
94 }
95
120 public bool AddProcess(Process process)
121 {
122 ArgumentNullException.ThrowIfNull(process);
123
124 if (_handle == IntPtr.Zero || _disposed)
125 {
126 return false;
127 }
128
129 try
130 {
131 return AssignProcessToJobObject(_handle, process.Handle);
132 }
133 catch (InvalidOperationException)
134 {
135 // 프로세스가 이미 종료된 경우 등.
136 return false;
137 }
138 }
139
148 public void Dispose()
149 {
150 if (_disposed)
151 {
152 return;
153 }
154
155 _disposed = true;
156
157 if (_handle != IntPtr.Zero)
158 {
160 _handle = IntPtr.Zero;
161 }
162 }
163
172 private const uint JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE = 0x00002000;
173
194
203 [StructLayout(LayoutKind.Sequential)]
205 {
232 public uint LimitFlags;
241 public UIntPtr MinimumWorkingSetSize;
250 public UIntPtr MaximumWorkingSetSize;
268 public UIntPtr Affinity;
277 public uint PriorityClass;
286 public uint SchedulingClass;
287 }
288
297 [StructLayout(LayoutKind.Sequential)]
298 private struct IO_COUNTERS
299 {
308 public ulong ReadOperationCount;
335 public ulong ReadTransferCount;
344 public ulong WriteTransferCount;
353 public ulong OtherTransferCount;
354 }
355
364 [StructLayout(LayoutKind.Sequential)]
422
455 [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
456 private static extern IntPtr CreateJobObject(IntPtr lpJobAttributes, string? lpName);
457
506 [DllImport("kernel32.dll", SetLastError = true)]
507 [return: MarshalAs(UnmanagedType.Bool)]
508 private static extern bool SetInformationJobObject(
509 IntPtr hJob,
510 JobObjectInfoType infoType,
511 IntPtr lpJobObjectInfo,
512 uint cbJobObjectInfoLength);
513
546 [DllImport("kernel32.dll", SetLastError = true)]
547 [return: MarshalAs(UnmanagedType.Bool)]
548 private static extern bool AssignProcessToJobObject(IntPtr hJob, IntPtr hProcess);
549
574 [DllImport("kernel32.dll", SetLastError = true)]
575 [return: MarshalAs(UnmanagedType.Bool)]
576 private static extern bool CloseHandle(IntPtr hObject);
577}
static bool AssignProcessToJobObject(IntPtr hJob, IntPtr hProcess)
static IntPtr CreateJobObject(IntPtr lpJobAttributes, string? lpName)
static bool SetInformationJobObject(IntPtr hJob, JobObjectInfoType infoType, IntPtr lpJobObjectInfo, uint cbJobObjectInfoLength)
static bool CloseHandle(IntPtr hObject)