Guest User

Untitled

a guest
Jan 24th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.41 KB | None | 0 0
  1. using System;
  2. using System.ComponentModel;
  3. using System.Diagnostics;
  4. using System.Threading.Tasks;
  5. using System.Runtime.InteropServices;
  6.  
  7. namespace ConsoleApp1
  8. {
  9. class Program
  10. {
  11. [DllImport("Kernel32.dll", CharSet = CharSet.Ansi)]
  12. private static extern IntPtr CreateJobObject(IntPtr jobAttributes, string name);
  13.  
  14. [DllImport("Kernel32.dll", SetLastError = true)]
  15. private static extern bool AssignProcessToJobObject(IntPtr job, IntPtr process);
  16.  
  17. private enum JobObjectInfoClass
  18. {
  19. ExtendedLimitInformation = 9,
  20. }
  21.  
  22. private enum JobObjectLimitFlags : uint
  23. {
  24. KillOnJobClose = 0x00002000
  25. }
  26.  
  27. [DllImport("Kernel32.dll", SetLastError = true)]
  28. private static extern bool SetInformationJobObject(IntPtr job, JobObjectInfoClass jobObjectInfoClass,
  29. IntPtr jobObjectInfo, uint jobObjectInfoLength);
  30.  
  31. [StructLayout(LayoutKind.Sequential)]
  32. class JobObjectBasicLimitInformation
  33. {
  34. public long PerProcessUserTimeLimit;
  35. public long PerJobUserTimeLimit;
  36. public JobObjectLimitFlags LimitFlags;
  37. public ulong MinimumWorkingSetSize;
  38. public ulong MaximumWorkingSetSize;
  39. public uint ActiveProcessLimit;
  40. public ulong Affinity;
  41. public uint PriorityClass;
  42. public uint SchedulingClass;
  43. }
  44.  
  45. [StructLayout(LayoutKind.Sequential)]
  46. class IoCounters
  47. {
  48. public ulong ReadOperationCount;
  49. public ulong WriteOperationCount;
  50. public ulong OtherOperationCount;
  51. public ulong ReadTransferCount;
  52. public ulong WriteTransferCount;
  53. public ulong OtherTransferCount;
  54. }
  55.  
  56. [StructLayout(LayoutKind.Sequential)]
  57. class JobObjectExtendedLimitInformation
  58. {
  59. public JobObjectBasicLimitInformation BasicLimitInformation;
  60. public IoCounters IoInfo;
  61. public ulong ProcessMemoryLimit;
  62. public ulong JobMemoryLimit;
  63. public ulong PeakProcessMemoryUsed;
  64. public ulong PeakJobMemoryUsed;
  65. }
  66.  
  67. static void Main(string[] args)
  68. {
  69. // Rely on the handle being destroyed at the end of the process
  70. var jobObject = CreateJobObjectWithKillOnClose();
  71. if (!AssignProcessToJobObject(jobObject, Process.GetCurrentProcess().Handle))
  72. {
  73. throw new Win32Exception();
  74. }
  75.  
  76. Console.CancelKeyPress += (_, e) =>
  77. {
  78. e.Cancel = false;
  79. };
  80. var p = new Process();
  81. p.StartInfo.RedirectStandardOutput = true;
  82. p.StartInfo.CreateNoWindow = true;
  83. p.StartInfo.UseShellExecute = false;
  84. p.StartInfo.FileName = Environment.ExpandEnvironmentVariables("%ComSpec%");
  85. p.StartInfo.Arguments = string.Join(" ", new[]
  86. {
  87. "/c",
  88. "\"for /l %x in (1, 1, 1000) do (echo piyo %x && timeout 1)\""
  89. });
  90. p.Start();
  91.  
  92. var t = Task.Run(async () =>
  93. {
  94. while (true)
  95. {
  96. var line = await p.StandardOutput.ReadLineAsync().ConfigureAwait(false);
  97. if (line == null) return;
  98. Console.WriteLine(line);
  99. }
  100. });
  101. t.Wait();
  102. Console.Error.WriteLine("Program Finished.");
  103. }
  104.  
  105. private static IntPtr CreateJobObjectWithKillOnClose()
  106. {
  107. IntPtr jobObject = CreateJobObject(IntPtr.Zero, null);
  108. var info = new JobObjectExtendedLimitInformation
  109. {
  110. BasicLimitInformation = new JobObjectBasicLimitInformation
  111. {
  112. LimitFlags = JobObjectLimitFlags.KillOnJobClose
  113. }
  114. };
  115. var infoSize = Marshal.SizeOf(info);
  116. var infoPtr = Marshal.AllocCoTaskMem(infoSize);
  117. Marshal.StructureToPtr(info, infoPtr, true);
  118. var ok = SetInformationJobObject(jobObject,
  119. JobObjectInfoClass.ExtendedLimitInformation,
  120. infoPtr,
  121. (uint) infoSize);
  122. if (!ok)
  123. {
  124. throw new Win32Exception();
  125. }
  126. Marshal.FreeCoTaskMem(infoPtr);
  127. return jobObject;
  128. }
  129. }
  130. }
Add Comment
Please, Sign In to add comment