Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.ComponentModel;
- using System.Diagnostics;
- using System.Threading.Tasks;
- using System.Runtime.InteropServices;
- namespace ConsoleApp1
- {
- class Program
- {
- [DllImport("Kernel32.dll", CharSet = CharSet.Ansi)]
- private static extern IntPtr CreateJobObject(IntPtr jobAttributes, string name);
- [DllImport("Kernel32.dll", SetLastError = true)]
- private static extern bool AssignProcessToJobObject(IntPtr job, IntPtr process);
- private enum JobObjectInfoClass
- {
- ExtendedLimitInformation = 9,
- }
- private enum JobObjectLimitFlags : uint
- {
- KillOnJobClose = 0x00002000
- }
- [DllImport("Kernel32.dll", SetLastError = true)]
- private static extern bool SetInformationJobObject(IntPtr job, JobObjectInfoClass jobObjectInfoClass,
- IntPtr jobObjectInfo, uint jobObjectInfoLength);
- [StructLayout(LayoutKind.Sequential)]
- class JobObjectBasicLimitInformation
- {
- public long PerProcessUserTimeLimit;
- public long PerJobUserTimeLimit;
- public JobObjectLimitFlags LimitFlags;
- public ulong MinimumWorkingSetSize;
- public ulong MaximumWorkingSetSize;
- public uint ActiveProcessLimit;
- public ulong Affinity;
- public uint PriorityClass;
- public uint SchedulingClass;
- }
- [StructLayout(LayoutKind.Sequential)]
- class IoCounters
- {
- public ulong ReadOperationCount;
- public ulong WriteOperationCount;
- public ulong OtherOperationCount;
- public ulong ReadTransferCount;
- public ulong WriteTransferCount;
- public ulong OtherTransferCount;
- }
- [StructLayout(LayoutKind.Sequential)]
- class JobObjectExtendedLimitInformation
- {
- public JobObjectBasicLimitInformation BasicLimitInformation;
- public IoCounters IoInfo;
- public ulong ProcessMemoryLimit;
- public ulong JobMemoryLimit;
- public ulong PeakProcessMemoryUsed;
- public ulong PeakJobMemoryUsed;
- }
- static void Main(string[] args)
- {
- // Rely on the handle being destroyed at the end of the process
- var jobObject = CreateJobObjectWithKillOnClose();
- if (!AssignProcessToJobObject(jobObject, Process.GetCurrentProcess().Handle))
- {
- throw new Win32Exception();
- }
- Console.CancelKeyPress += (_, e) =>
- {
- e.Cancel = false;
- };
- var p = new Process();
- p.StartInfo.RedirectStandardOutput = true;
- p.StartInfo.CreateNoWindow = true;
- p.StartInfo.UseShellExecute = false;
- p.StartInfo.FileName = Environment.ExpandEnvironmentVariables("%ComSpec%");
- p.StartInfo.Arguments = string.Join(" ", new[]
- {
- "/c",
- "\"for /l %x in (1, 1, 1000) do (echo piyo %x && timeout 1)\""
- });
- p.Start();
- var t = Task.Run(async () =>
- {
- while (true)
- {
- var line = await p.StandardOutput.ReadLineAsync().ConfigureAwait(false);
- if (line == null) return;
- Console.WriteLine(line);
- }
- });
- t.Wait();
- Console.Error.WriteLine("Program Finished.");
- }
- private static IntPtr CreateJobObjectWithKillOnClose()
- {
- IntPtr jobObject = CreateJobObject(IntPtr.Zero, null);
- var info = new JobObjectExtendedLimitInformation
- {
- BasicLimitInformation = new JobObjectBasicLimitInformation
- {
- LimitFlags = JobObjectLimitFlags.KillOnJobClose
- }
- };
- var infoSize = Marshal.SizeOf(info);
- var infoPtr = Marshal.AllocCoTaskMem(infoSize);
- Marshal.StructureToPtr(info, infoPtr, true);
- var ok = SetInformationJobObject(jobObject,
- JobObjectInfoClass.ExtendedLimitInformation,
- infoPtr,
- (uint) infoSize);
- if (!ok)
- {
- throw new Win32Exception();
- }
- Marshal.FreeCoTaskMem(infoPtr);
- return jobObject;
- }
- }
- }
Add Comment
Please, Sign In to add comment