Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
Process.cs
Go to the documentation of this file.
3using System.IO;
9using System.Text;
13
14namespace System.Diagnostics;
15
16[Designer("System.Diagnostics.Design.ProcessDesigner, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
18{
19 private enum StreamReadMode
20 {
24 }
25
26 private enum State
27 {
28 HaveId = 1,
29 IsLocal = 2,
32 Exited = 16,
33 Associated = 32
34 }
35
36 internal class ShellExecuteHelper
37 {
38 private unsafe readonly global::Interop.Shell32.SHELLEXECUTEINFO* _executeInfo;
39
40 private bool _succeeded;
41
42 private bool _notpresent;
43
44 public int ErrorCode { get; private set; }
45
46 public unsafe ShellExecuteHelper(global::Interop.Shell32.SHELLEXECUTEINFO* executeInfo)
47 {
49 }
50
51 private unsafe void ShellExecuteFunction()
52 {
53 try
54 {
55 if (!(_succeeded = global::Interop.Shell32.ShellExecuteExW(_executeInfo)))
56 {
58 }
59 }
61 {
62 _notpresent = true;
63 }
64 }
65
67 {
68 if (Thread.CurrentThread.GetApartmentState() != 0)
69 {
72 {
73 IsBackground = true,
74 Name = ".NET Process STA"
75 };
76 thread.SetApartmentState(ApartmentState.STA);
77 thread.Start();
78 thread.Join();
79 }
80 else
81 {
83 }
84 if (_notpresent)
85 {
87 }
88 return _succeeded;
89 }
90 }
91
92 private bool _haveProcessId;
93
94 private int _processId;
95
96 private bool _haveProcessHandle;
97
99
100 private bool _isRemoteMachine;
101
102 private string _machineName;
103
105
107
109
111
113
115
117
119
120 private bool _havePriorityClass;
121
123
125
126 private bool _watchForExit;
127
128 private bool _watchingForExit;
129
130 private EventHandler _onExited;
131
132 private bool _exited;
133
134 private int _exitCode;
135
137
139
140 private bool _haveExitTime;
141
143
145
146 private bool _raisedOnExited;
147
149
151
153
155
157
158 private bool _disposed;
159
161
163
165
167
169
170 internal bool _pendingOutputRead;
171
172 internal bool _pendingErrorRead;
173
174 private static int s_cachedSerializationSwitch;
175
176 private static readonly object s_createProcessLock = new object();
177
178 private bool _signaled;
179
180 private bool _haveMainWindow;
181
183
184 private string _mainWindowTitle;
185
186 private bool _haveResponding;
187
188 private bool _responding;
189
191 {
192 get
193 {
194 EnsureState(State.Associated);
195 return GetOrOpenProcessHandle();
196 }
197 }
198
200
201 private bool Associated
202 {
203 get
204 {
205 if (!_haveProcessId)
206 {
207 return _haveProcessHandle;
208 }
209 return true;
210 }
211 }
212
213 public int BasePriority
214 {
215 get
216 {
217 EnsureState(State.HaveProcessInfo);
219 }
220 }
221
222 public int ExitCode
223 {
224 get
225 {
226 EnsureState(State.Exited);
227 return _exitCode;
228 }
229 }
230
231 public bool HasExited
232 {
233 get
234 {
235 if (!_exited)
236 {
237 EnsureState(State.Associated);
239 if (_exited)
240 {
242 }
243 }
244 return _exited;
245 }
246 }
247
249 {
250 get
251 {
252 if (!_startTime.HasValue)
253 {
255 }
256 return _startTime.Value;
257 }
258 }
259
261 {
262 get
263 {
264 if (!_haveExitTime)
265 {
266 EnsureState(State.Exited);
268 _haveExitTime = true;
269 }
270 return _exitTime;
271 }
272 }
273
274 public int Id
275 {
276 get
277 {
278 EnsureState(State.HaveId);
279 return _processId;
280 }
281 }
282
283 public string MachineName
284 {
285 get
286 {
287 EnsureState(State.Associated);
288 return _machineName;
289 }
290 }
291
293 {
294 [UnsupportedOSPlatform("ios")]
295 [UnsupportedOSPlatform("tvos")]
296 get
297 {
299 return _maxWorkingSet;
300 }
301 [SupportedOSPlatform("windows")]
302 [SupportedOSPlatform("macos")]
303 [SupportedOSPlatform("freebsd")]
304 set
305 {
307 }
308 }
309
311 {
312 [UnsupportedOSPlatform("ios")]
313 [UnsupportedOSPlatform("tvos")]
314 get
315 {
317 return _minWorkingSet;
318 }
319 [SupportedOSPlatform("windows")]
320 [SupportedOSPlatform("macos")]
321 [SupportedOSPlatform("freebsd")]
322 set
323 {
325 }
326 }
327
329 {
330 get
331 {
332 if (_modules == null)
333 {
334 EnsureState((State)7);
336 }
337 return _modules;
338 }
339 }
340
342 {
343 get
344 {
345 EnsureState(State.HaveProcessInfo);
347 }
348 }
349
350 [Obsolete("Process.NonpagedSystemMemorySize has been deprecated because the type of the property can't represent all valid results. Use System.Diagnostics.Process.NonpagedSystemMemorySize64 instead.")]
352 {
353 get
354 {
355 EnsureState(State.HaveProcessInfo);
357 }
358 }
359
361 {
362 get
363 {
364 EnsureState(State.HaveProcessInfo);
366 }
367 }
368
369 [Obsolete("Process.PagedMemorySize has been deprecated because the type of the property can't represent all valid results. Use System.Diagnostics.Process.PagedMemorySize64 instead.")]
371 {
372 get
373 {
374 EnsureState(State.HaveProcessInfo);
375 return (int)_processInfo.PageFileBytes;
376 }
377 }
378
380 {
381 get
382 {
383 EnsureState(State.HaveProcessInfo);
385 }
386 }
387
388 [Obsolete("Process.PagedSystemMemorySize has been deprecated because the type of the property can't represent all valid results. Use System.Diagnostics.Process.PagedSystemMemorySize64 instead.")]
390 {
391 get
392 {
393 EnsureState(State.HaveProcessInfo);
394 return (int)_processInfo.PoolPagedBytes;
395 }
396 }
397
399 {
400 get
401 {
402 EnsureState(State.HaveProcessInfo);
404 }
405 }
406
407 [Obsolete("Process.PeakPagedMemorySize has been deprecated because the type of the property can't represent all valid results. Use System.Diagnostics.Process.PeakPagedMemorySize64 instead.")]
409 {
410 get
411 {
412 EnsureState(State.HaveProcessInfo);
414 }
415 }
416
418 {
419 get
420 {
421 EnsureState(State.HaveProcessInfo);
423 }
424 }
425
426 [Obsolete("Process.PeakWorkingSet has been deprecated because the type of the property can't represent all valid results. Use System.Diagnostics.Process.PeakWorkingSet64 instead.")]
427 public int PeakWorkingSet
428 {
429 get
430 {
431 EnsureState(State.HaveProcessInfo);
432 return (int)_processInfo.WorkingSetPeak;
433 }
434 }
435
437 {
438 get
439 {
440 EnsureState(State.HaveProcessInfo);
442 }
443 }
444
445 [Obsolete("Process.PeakVirtualMemorySize has been deprecated because the type of the property can't represent all valid results. Use System.Diagnostics.Process.PeakVirtualMemorySize64 instead.")]
447 {
448 get
449 {
450 EnsureState(State.HaveProcessInfo);
451 return (int)_processInfo.VirtualBytesPeak;
452 }
453 }
454
456 {
457 get
458 {
460 {
463 }
465 }
466 set
467 {
471 }
472 }
473
475 {
476 get
477 {
479 {
481 _havePriorityClass = true;
482 }
483 return _priorityClass;
484 }
485 set
486 {
488 {
490 }
493 _havePriorityClass = true;
494 }
495 }
496
498 {
499 get
500 {
501 EnsureState(State.HaveProcessInfo);
503 }
504 }
505
506 [Obsolete("Process.PrivateMemorySize has been deprecated because the type of the property can't represent all valid results. Use System.Diagnostics.Process.PrivateMemorySize64 instead.")]
508 {
509 get
510 {
511 EnsureState(State.HaveProcessInfo);
512 return (int)_processInfo.PrivateBytes;
513 }
514 }
515
516 public string ProcessName
517 {
518 get
519 {
520 EnsureState(State.HaveProcessInfo);
522 }
523 }
524
525 [SupportedOSPlatform("windows")]
526 [SupportedOSPlatform("linux")]
528 {
529 get
530 {
532 {
535 }
536 return _processorAffinity;
537 }
538 set
539 {
543 }
544 }
545
546 public int SessionId
547 {
548 get
549 {
550 EnsureState(State.HaveProcessInfo);
551 return _processInfo.SessionId;
552 }
553 }
554
556 {
557 get
558 {
559 if (_startInfo == null)
560 {
561 if (Associated)
562 {
564 }
566 }
567 return _startInfo;
568 }
569 set
570 {
571 if (value == null)
572 {
573 throw new ArgumentNullException("value");
574 }
575 if (Associated)
576 {
578 }
580 }
581 }
582
584 {
585 get
586 {
587 if (_threads == null)
588 {
589 EnsureState(State.HaveProcessInfo);
592 for (int i = 0; i < count; i++)
593 {
595 }
598 }
599 return _threads;
600 }
601 }
602
603 public int HandleCount
604 {
605 get
606 {
607 EnsureState(State.HaveProcessInfo);
609 }
610 }
611
613 {
614 get
615 {
616 EnsureState(State.HaveProcessInfo);
618 }
619 }
620
621 [Obsolete("Process.VirtualMemorySize has been deprecated because the type of the property can't represent all valid results. Use System.Diagnostics.Process.VirtualMemorySize64 instead.")]
623 {
624 get
625 {
626 EnsureState(State.HaveProcessInfo);
627 return (int)_processInfo.VirtualBytes;
628 }
629 }
630
632 {
633 get
634 {
635 return _watchForExit;
636 }
637 set
638 {
639 if (value == _watchForExit)
640 {
641 return;
642 }
643 if (Associated)
644 {
645 if (value)
646 {
648 }
649 else
650 {
652 }
653 }
655 }
656 }
657
659 {
660 get
661 {
663 if (_standardInput == null)
664 {
666 }
668 return _standardInput;
669 }
670 }
671
673 {
674 get
675 {
677 if (_standardOutput == null)
678 {
680 }
681 if (_outputStreamReadMode == StreamReadMode.Undefined)
682 {
684 }
685 else if (_outputStreamReadMode != StreamReadMode.SyncMode)
686 {
688 }
689 return _standardOutput;
690 }
691 }
692
694 {
695 get
696 {
698 if (_standardError == null)
699 {
701 }
702 if (_errorStreamReadMode == StreamReadMode.Undefined)
703 {
705 }
706 else if (_errorStreamReadMode != StreamReadMode.SyncMode)
707 {
709 }
710 return _standardError;
711 }
712 }
713
714 public long WorkingSet64
715 {
716 get
717 {
718 EnsureState(State.HaveProcessInfo);
720 }
721 }
722
723 [Obsolete("Process.WorkingSet has been deprecated because the type of the property can't represent all valid results. Use System.Diagnostics.Process.WorkingSet64 instead.")]
724 public int WorkingSet
725 {
726 get
727 {
728 EnsureState(State.HaveProcessInfo);
729 return (int)_processInfo.WorkingSet;
730 }
731 }
732
734
736 {
737 get
738 {
739 EnsureState((State)3);
741 }
742 }
743
745
747
749
751
753
755 {
756 get
757 {
759 if (!global::Interop.Kernel32.GetProcessPriorityBoost(handle, out var disabled))
760 {
761 throw new Win32Exception();
762 }
763 return !disabled;
764 }
765 set
766 {
768 if (!global::Interop.Kernel32.SetProcessPriorityBoost(handle, !value))
769 {
770 throw new Win32Exception();
771 }
772 }
773 }
774
776 {
777 get
778 {
780 int priorityClass = global::Interop.Kernel32.GetPriorityClass(handle);
781 if (priorityClass == 0)
782 {
783 throw new Win32Exception();
784 }
786 }
787 set
788 {
790 if (!global::Interop.Kernel32.SetPriorityClass(handle, (int)value))
791 {
792 throw new Win32Exception();
793 }
794 }
795 }
796
798 {
799 get
800 {
802 if (!global::Interop.Kernel32.GetProcessAffinityMask(handle, out var processMask, out var _))
803 {
804 throw new Win32Exception();
805 }
806 return processMask;
807 }
808 set
809 {
811 if (!global::Interop.Kernel32.SetProcessAffinityMask(handle, value))
812 {
813 throw new Win32Exception();
814 }
815 }
816 }
817
831
832 public string MainWindowTitle
833 {
834 get
835 {
836 if (_mainWindowTitle == null)
837 {
839 }
840 return _mainWindowTitle;
841 }
842 }
843
844 public bool Responding
845 {
846 get
847 {
848 if (!_haveResponding)
849 {
851 _haveResponding = true;
852 }
853 return _responding;
854 }
855 }
856
857 private unsafe int ParentProcessId
858 {
859 get
860 {
861 using SafeProcessHandle processHandle = GetProcessHandle(1024);
862 Unsafe.SkipInit(out global::Interop.NtDll.PROCESS_BASIC_INFORMATION pROCESS_BASIC_INFORMATION);
863 if (global::Interop.NtDll.NtQueryInformationProcess(processHandle, 0, &pROCESS_BASIC_INFORMATION, (uint)sizeof(global::Interop.NtDll.PROCESS_BASIC_INFORMATION), out var _) != 0)
864 {
866 }
867 return (int)(uint)pROCESS_BASIC_INFORMATION.InheritedFromUniqueProcessId;
868 }
869 }
870
872
874
875 public event EventHandler Exited
876 {
877 add
878 {
880 }
881 remove
882 {
884 }
885 }
886
887 public Process()
888 {
889 if (GetType() == typeof(Process))
890 {
891 GC.SuppressFinalize(this);
892 }
893 _machineName = ".";
896 }
897
909
911 {
912 lock (this)
913 {
915 {
918 }
919 }
920 }
921
922 protected override void Dispose(bool disposing)
923 {
924 if (!_disposed)
925 {
926 if (disposing)
927 {
928 Close();
929 }
930 _disposed = true;
931 }
932 }
933
934 public bool CloseMainWindow()
935 {
936 return CloseMainWindowCore();
937 }
938
939 public bool WaitForInputIdle()
940 {
941 return WaitForInputIdle(int.MaxValue);
942 }
943
945 {
947 }
948
949 public void Close()
950 {
951 if (!Associated)
952 {
953 return;
954 }
955 lock (this)
956 {
958 }
960 {
962 _processHandle = null;
963 _haveProcessHandle = false;
964 }
965 _haveProcessId = false;
966 _isRemoteMachine = false;
967 _machineName = ".";
968 _raisedOnExited = false;
969 try
970 {
972 {
973 if (_outputStreamReadMode == StreamReadMode.AsyncMode)
974 {
976 _output?.Dispose();
977 }
979 }
980 if (_standardError != null && (_errorStreamReadMode == StreamReadMode.AsyncMode || _errorStreamReadMode == StreamReadMode.Undefined))
981 {
982 if (_errorStreamReadMode == StreamReadMode.AsyncMode)
983 {
985 _error?.Dispose();
986 }
988 }
990 {
992 }
993 }
994 finally
995 {
996 _standardOutput = null;
997 _standardInput = null;
998 _standardError = null;
999 _output = null;
1000 _error = null;
1001 CloseCore();
1002 Refresh();
1003 }
1004 }
1005
1007 {
1008 if ((state & State.Associated) != 0 && !Associated)
1009 {
1011 }
1012 if ((state & State.HaveId) != 0)
1013 {
1014 if (!_haveProcessId)
1015 {
1016 if (!_haveProcessHandle)
1017 {
1018 EnsureState(State.Associated);
1020 }
1022 }
1023 _ = state & State.HaveNonExitedId;
1024 _ = 5;
1025 }
1026 if ((state & State.IsLocal) != 0 && _isRemoteMachine)
1027 {
1029 }
1030 if ((state & State.HaveProcessInfo) != 0 && _processInfo == null)
1031 {
1032 if ((state & State.HaveNonExitedId) != State.HaveNonExitedId)
1033 {
1034 EnsureState(State.HaveNonExitedId);
1035 }
1037 if (_processInfo == null)
1038 {
1040 }
1041 }
1042 if ((state & State.Exited) != 0)
1043 {
1044 if (!HasExited)
1045 {
1047 }
1048 if (!_haveProcessHandle)
1049 {
1051 }
1052 }
1053 }
1054
1056 {
1058 {
1060 _haveWorkingSetLimits = true;
1061 }
1062 }
1063
1069
1078
1080 {
1081 return GetProcessById(processId, ".");
1082 }
1083
1084 public static Process[] GetProcessesByName(string? processName)
1085 {
1086 return GetProcessesByName(processName, ".");
1087 }
1088
1089 public static Process[] GetProcesses()
1090 {
1091 return GetProcesses(".");
1092 }
1093
1094 public static Process[] GetProcesses(string machineName)
1095 {
1098 Process[] array = new Process[processInfos.Length];
1099 for (int i = 0; i < processInfos.Length; i++)
1100 {
1103 }
1104 return array;
1105 }
1106
1108 {
1109 return new Process(".", isRemoteMachine: false, Environment.ProcessId, null);
1110 }
1111
1112 protected void OnExited()
1113 {
1114 EventHandler onExited = _onExited;
1115 if (onExited != null)
1116 {
1118 if (synchronizingObject != null && synchronizingObject.InvokeRequired)
1119 {
1120 synchronizingObject.BeginInvoke(onExited, new object[2]
1121 {
1122 this,
1124 });
1125 }
1126 else
1127 {
1128 onExited(this, EventArgs.Empty);
1129 }
1130 }
1131 }
1132
1133 private void RaiseOnExited()
1134 {
1135 if (_raisedOnExited)
1136 {
1137 return;
1138 }
1139 lock (this)
1140 {
1141 if (!_raisedOnExited)
1142 {
1143 _raisedOnExited = true;
1144 OnExited();
1145 }
1146 }
1147 }
1148
1149 public void Refresh()
1150 {
1151 _processInfo = null;
1152 _threads?.Dispose();
1153 _threads = null;
1154 _modules?.Dispose();
1155 _modules = null;
1156 _exited = false;
1157 _haveWorkingSetLimits = false;
1158 _haveProcessorAffinity = false;
1159 _havePriorityClass = false;
1160 _haveExitTime = false;
1162 RefreshCore();
1163 }
1164
1166 {
1167 if (!_haveProcessHandle)
1168 {
1169 CheckDisposed();
1171 }
1172 return _processHandle;
1173 }
1174
1175 private void SetProcessHandle(SafeProcessHandle processHandle)
1176 {
1177 _processHandle = processHandle;
1178 _haveProcessHandle = true;
1179 if (_watchForExit)
1180 {
1182 }
1183 }
1184
1185 private void SetProcessId(int processId)
1186 {
1188 _haveProcessId = true;
1189 }
1190
1191 [UnsupportedOSPlatform("ios")]
1192 [UnsupportedOSPlatform("tvos")]
1193 public bool Start()
1194 {
1195 Close();
1197 if (startInfo.FileName.Length == 0)
1198 {
1200 }
1201 if (startInfo.StandardInputEncoding != null && !startInfo.RedirectStandardInput)
1202 {
1204 }
1205 if (startInfo.StandardOutputEncoding != null && !startInfo.RedirectStandardOutput)
1206 {
1208 }
1209 if (startInfo.StandardErrorEncoding != null && !startInfo.RedirectStandardError)
1210 {
1212 }
1213 if (!string.IsNullOrEmpty(startInfo.Arguments) && startInfo.HasArgumentList)
1214 {
1216 }
1217 CheckDisposed();
1219 return StartCore(startInfo);
1220 }
1221
1222 [UnsupportedOSPlatform("ios")]
1223 [UnsupportedOSPlatform("tvos")]
1224 public static Process Start(string fileName)
1225 {
1226 return Start(new ProcessStartInfo(fileName));
1227 }
1228
1229 [UnsupportedOSPlatform("ios")]
1230 [UnsupportedOSPlatform("tvos")]
1231 public static Process Start(string fileName, string arguments)
1232 {
1234 }
1235
1236 [UnsupportedOSPlatform("ios")]
1237 [UnsupportedOSPlatform("tvos")]
1239 {
1240 if (fileName == null)
1241 {
1242 throw new ArgumentNullException("fileName");
1243 }
1244 if (arguments == null)
1245 {
1246 throw new ArgumentNullException("arguments");
1247 }
1249 foreach (string argument in arguments)
1250 {
1251 processStartInfo.ArgumentList.Add(argument);
1252 }
1253 return Start(processStartInfo);
1254 }
1255
1256 [UnsupportedOSPlatform("ios")]
1257 [UnsupportedOSPlatform("tvos")]
1259 {
1260 Process process = new Process();
1261 if (startInfo == null)
1262 {
1263 throw new ArgumentNullException("startInfo");
1264 }
1266 if (!process.Start())
1267 {
1268 return null;
1269 }
1270 return process;
1271 }
1272
1273 private void StopWatchingForExit()
1274 {
1275 if (!_watchingForExit)
1276 {
1277 return;
1278 }
1280 WaitHandle waitHandle = null;
1281 lock (this)
1282 {
1283 if (_watchingForExit)
1284 {
1285 _watchingForExit = false;
1287 _waitHandle = null;
1289 _registeredWaitHandle = null;
1290 }
1291 }
1292 registeredWaitHandle?.Unregister(null);
1293 waitHandle?.Dispose();
1294 }
1295
1296 public override string ToString()
1297 {
1298 string text = base.ToString();
1299 try
1300 {
1301 if (Associated)
1302 {
1303 if (_processInfo == null)
1304 {
1306 }
1307 if (_processInfo != null)
1308 {
1310 if (processName.Length != 0)
1311 {
1312 text = text + " (" + processName + ")";
1313 }
1314 }
1315 }
1316 }
1317 catch
1318 {
1319 }
1320 return text;
1321 }
1322
1323 public void WaitForExit()
1324 {
1325 WaitForExit(-1);
1326 }
1327
1328 public bool WaitForExit(int milliseconds)
1329 {
1330 bool flag = WaitForExitCore(milliseconds);
1331 if (flag && _watchForExit)
1332 {
1333 RaiseOnExited();
1334 }
1335 return flag;
1336 }
1337
1339 {
1340 if (!Associated)
1341 {
1343 }
1344 if (!HasExited)
1345 {
1346 cancellationToken.ThrowIfCancellationRequested();
1347 }
1348 try
1349 {
1350 EnableRaisingEvents = true;
1351 }
1353 {
1354 if (!HasExited)
1355 {
1356 throw;
1357 }
1359 return;
1360 }
1361 TaskCompletionSource tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
1362 EventHandler handler = delegate
1363 {
1364 tcs.TrySetResult();
1365 };
1366 Exited += handler;
1367 try
1368 {
1369 if (!HasExited)
1370 {
1371 using (cancellationToken.UnsafeRegister(delegate(object s, CancellationToken cancellationToken)
1372 {
1373 ((TaskCompletionSource)s).TrySetCanceled(cancellationToken);
1374 }, tcs))
1375 {
1376 await tcs.Task.ConfigureAwait(continueOnCapturedContext: false);
1377 }
1378 }
1380 }
1381 finally
1382 {
1383 Exited -= handler;
1384 }
1386 {
1387 if (_output != null)
1388 {
1390 }
1391 if (_error != null)
1392 {
1394 }
1395 }
1396 }
1397
1399 {
1400 if (_outputStreamReadMode == StreamReadMode.Undefined)
1401 {
1403 }
1404 else if (_outputStreamReadMode != StreamReadMode.AsyncMode)
1405 {
1407 }
1409 {
1411 }
1412 _pendingOutputRead = true;
1413 if (_output == null)
1414 {
1415 if (_standardOutput == null)
1416 {
1418 }
1421 }
1423 }
1424
1426 {
1427 if (_errorStreamReadMode == StreamReadMode.Undefined)
1428 {
1430 }
1431 else if (_errorStreamReadMode != StreamReadMode.AsyncMode)
1432 {
1434 }
1436 {
1438 }
1439 _pendingErrorRead = true;
1440 if (_error == null)
1441 {
1442 if (_standardError == null)
1443 {
1445 }
1448 }
1450 }
1451
1452 public void CancelOutputRead()
1453 {
1454 CheckDisposed();
1455 if (_output != null)
1456 {
1458 _pendingOutputRead = false;
1459 return;
1460 }
1462 }
1463
1464 public void CancelErrorRead()
1465 {
1466 CheckDisposed();
1467 if (_error != null)
1468 {
1470 _pendingErrorRead = false;
1471 return;
1472 }
1474 }
1475
1476 internal void OutputReadNotifyUser(string data)
1477 {
1479 if (outputDataReceived != null)
1480 {
1483 if (synchronizingObject != null && synchronizingObject.InvokeRequired)
1484 {
1485 synchronizingObject.Invoke(outputDataReceived, new object[2] { this, dataReceivedEventArgs });
1486 }
1487 else
1488 {
1490 }
1491 }
1492 }
1493
1494 internal void ErrorReadNotifyUser(string data)
1495 {
1497 if (errorDataReceived != null)
1498 {
1501 if (synchronizingObject != null && synchronizingObject.InvokeRequired)
1502 {
1503 synchronizingObject.Invoke(errorDataReceived, new object[2] { this, dataReceivedEventArgs });
1504 }
1505 else
1506 {
1508 }
1509 }
1510 }
1511
1512 private void CheckDisposed()
1513 {
1514 if (_disposed)
1515 {
1516 throw new ObjectDisposedException(GetType().Name);
1517 }
1518 }
1519
1521 {
1522 string p = (string.IsNullOrEmpty(workingDirectory) ? Directory.GetCurrentDirectory() : workingDirectory);
1524 return new Win32Exception(errorCode, message);
1525 }
1526
1527 public static Process[] GetProcessesByName(string? processName, string machineName)
1528 {
1529 if (processName == null)
1530 {
1531 processName = string.Empty;
1532 }
1535 for (int i = 0; i < processes.Length; i++)
1536 {
1537 if (string.Equals(processName, processes[i].ProcessName, StringComparison.OrdinalIgnoreCase))
1538 {
1539 list.Add(processes[i]);
1540 }
1541 else
1542 {
1543 processes[i].Dispose();
1544 }
1545 }
1546 return list.ToArray();
1547 }
1548
1549 [CLSCompliant(false)]
1550 [SupportedOSPlatform("windows")]
1560
1561 [CLSCompliant(false)]
1562 [SupportedOSPlatform("windows")]
1572
1573 public static void EnterDebugMode()
1574 {
1575 SetPrivilege("SeDebugPrivilege", 2);
1576 }
1577
1578 public static void LeaveDebugMode()
1579 {
1580 SetPrivilege("SeDebugPrivilege", 0);
1581 }
1582
1583 [UnsupportedOSPlatform("ios")]
1584 [UnsupportedOSPlatform("tvos")]
1585 public void Kill()
1586 {
1588 if (!safeProcessHandle.IsInvalid && !global::Interop.Kernel32.TerminateProcess(safeProcessHandle, -1))
1589 {
1591 if (ex.NativeErrorCode != 5 || !global::Interop.Kernel32.GetExitCodeProcess(safeProcessHandle, out var exitCode) || exitCode == 259)
1592 {
1593 throw ex;
1594 }
1595 }
1596 }
1597
1598 private void RefreshCore()
1599 {
1600 _signaled = false;
1601 _haveMainWindow = false;
1602 _mainWindowTitle = null;
1603 _haveResponding = false;
1604 }
1605
1606 private void CloseCore()
1607 {
1608 }
1609
1611 {
1612 if (_watchingForExit)
1613 {
1614 return;
1615 }
1616 lock (this)
1617 {
1618 if (!_watchingForExit)
1619 {
1620 _watchingForExit = true;
1621 try
1622 {
1623 _waitHandle = new global::Interop.Kernel32.ProcessWaitHandle(GetOrOpenProcessHandle());
1625 return;
1626 }
1627 catch
1628 {
1629 _watchingForExit = false;
1630 throw;
1631 }
1632 }
1633 }
1634 }
1635
1637 {
1639 try
1640 {
1642 if (safeProcessHandle.IsInvalid)
1643 {
1644 return true;
1645 }
1646 using global::Interop.Kernel32.ProcessWaitHandle processWaitHandle = new global::Interop.Kernel32.ProcessWaitHandle(safeProcessHandle);
1647 return _signaled = processWaitHandle.WaitOne(milliseconds);
1648 }
1649 finally
1650 {
1651 if (milliseconds == -1)
1652 {
1653 _output?.EOF.GetAwaiter().GetResult();
1654 _error?.EOF.GetAwaiter().GetResult();
1655 }
1656 safeProcessHandle?.Dispose();
1657 }
1658 }
1659
1660 private void UpdateHasExited()
1661 {
1663 if (safeProcessHandle.IsInvalid)
1664 {
1665 _exited = true;
1666 return;
1667 }
1668 if (global::Interop.Kernel32.GetExitCodeProcess(safeProcessHandle, out var exitCode) && exitCode != 259)
1669 {
1671 _exited = true;
1672 return;
1673 }
1674 if (!_signaled)
1675 {
1676 using global::Interop.Kernel32.ProcessWaitHandle processWaitHandle = new global::Interop.Kernel32.ProcessWaitHandle(safeProcessHandle);
1677 _signaled = processWaitHandle.WaitOne(0);
1678 }
1679 if (_signaled)
1680 {
1681 if (!global::Interop.Kernel32.GetExitCodeProcess(safeProcessHandle, out exitCode))
1682 {
1683 throw new Win32Exception();
1684 }
1686 _exited = true;
1687 }
1688 }
1689
1691 {
1692 return GetProcessHandle(2035711);
1693 }
1694
1696 {
1698 if (!global::Interop.Kernel32.GetProcessWorkingSetSizeEx(handle, out minWorkingSet, out maxWorkingSet, out var _))
1699 {
1700 throw new Win32Exception();
1701 }
1702 }
1703
1705 {
1707 if (!global::Interop.Kernel32.GetProcessWorkingSetSizeEx(handle, out var min, out var max, out var flags))
1708 {
1709 throw new Win32Exception();
1710 }
1711 if (newMin.HasValue)
1712 {
1713 min = newMin.Value;
1714 }
1715 if (newMax.HasValue)
1716 {
1717 max = newMax.Value;
1718 }
1719 if ((long)min > (long)max)
1720 {
1721 if (newMin.HasValue)
1722 {
1724 }
1726 }
1727 if (!global::Interop.Kernel32.SetProcessWorkingSetSizeEx(handle, min, max, 0))
1728 {
1729 throw new Win32Exception();
1730 }
1731 if (!global::Interop.Kernel32.GetProcessWorkingSetSizeEx(handle, out min, out max, out flags))
1732 {
1733 throw new Win32Exception();
1734 }
1735 resultingMin = min;
1736 resultingMax = max;
1737 }
1738
1740 {
1744 global::Interop.Kernel32.STARTUPINFO lpStartupInfo = default(global::Interop.Kernel32.STARTUPINFO);
1745 global::Interop.Kernel32.PROCESS_INFORMATION lpProcessInformation = default(global::Interop.Kernel32.PROCESS_INFORMATION);
1746 global::Interop.Kernel32.SECURITY_ATTRIBUTES procSecAttrs = default(global::Interop.Kernel32.SECURITY_ATTRIBUTES);
1755 {
1756 try
1757 {
1758 lpStartupInfo.cb = sizeof(global::Interop.Kernel32.STARTUPINFO);
1759 if (startInfo.RedirectStandardInput || startInfo.RedirectStandardOutput || startInfo.RedirectStandardError)
1760 {
1761 if (startInfo.RedirectStandardInput)
1762 {
1764 }
1765 else
1766 {
1767 childHandle = new SafeFileHandle(global::Interop.Kernel32.GetStdHandle(-10), ownsHandle: false);
1768 }
1769 if (startInfo.RedirectStandardOutput)
1770 {
1772 }
1773 else
1774 {
1775 childHandle2 = new SafeFileHandle(global::Interop.Kernel32.GetStdHandle(-11), ownsHandle: false);
1776 }
1777 if (startInfo.RedirectStandardError)
1778 {
1780 }
1781 else
1782 {
1783 childHandle3 = new SafeFileHandle(global::Interop.Kernel32.GetStdHandle(-12), ownsHandle: false);
1784 }
1785 lpStartupInfo.hStdInput = childHandle.DangerousGetHandle();
1786 lpStartupInfo.hStdOutput = childHandle2.DangerousGetHandle();
1787 lpStartupInfo.hStdError = childHandle3.DangerousGetHandle();
1789 }
1790 int num = 0;
1791 if (startInfo.CreateNoWindow)
1792 {
1793 num |= 0x8000000;
1794 }
1795 string text = null;
1796 if (startInfo._environmentVariables != null)
1797 {
1798 num |= 0x400;
1799 text = GetEnvironmentVariablesBlock(startInfo._environmentVariables);
1800 }
1801 string text2 = startInfo.WorkingDirectory;
1802 if (text2.Length == 0)
1803 {
1804 text2 = null;
1805 }
1806 int num2 = 0;
1807 bool flag;
1808 if (startInfo.UserName.Length != 0)
1809 {
1810 if (startInfo.Password != null && startInfo.PasswordInClearText != null)
1811 {
1813 }
1814 global::Interop.Advapi32.LogonFlags logonFlags = (global::Interop.Advapi32.LogonFlags)0;
1815 if (startInfo.LoadUserProfile)
1816 {
1817 logonFlags = global::Interop.Advapi32.LogonFlags.LOGON_WITH_PROFILE;
1818 }
1819 fixed (char* ptr = startInfo.PasswordInClearText ?? string.Empty)
1820 {
1821 fixed (char* ptr2 = text)
1822 {
1823 fixed (char* cmdLine = &commandLine.GetPinnableReference(terminate: true))
1824 {
1826 try
1827 {
1828 flag = global::Interop.Advapi32.CreateProcessWithLogonW(startInfo.UserName, startInfo.Domain, (intPtr != IntPtr.Zero) ? intPtr : ((IntPtr)ptr), logonFlags, null, cmdLine, num, (IntPtr)ptr2, text2, ref lpStartupInfo, ref lpProcessInformation);
1829 if (!flag)
1830 {
1832 }
1833 }
1834 finally
1835 {
1836 if (intPtr != IntPtr.Zero)
1837 {
1839 }
1840 }
1841 }
1842 }
1843 }
1844 }
1845 else
1846 {
1847 fixed (char* ptr3 = text)
1848 {
1849 fixed (char* lpCommandLine = &commandLine.GetPinnableReference(terminate: true))
1850 {
1851 flag = global::Interop.Kernel32.CreateProcess(null, lpCommandLine, ref procSecAttrs, ref procSecAttrs, bInheritHandles: true, num, (IntPtr)ptr3, text2, ref lpStartupInfo, ref lpProcessInformation);
1852 if (!flag)
1853 {
1855 }
1856 }
1857 }
1858 }
1859 if (lpProcessInformation.hProcess != IntPtr.Zero && lpProcessInformation.hProcess != new IntPtr(-1))
1860 {
1862 }
1863 if (lpProcessInformation.hThread != IntPtr.Zero && lpProcessInformation.hThread != new IntPtr(-1))
1864 {
1865 global::Interop.Kernel32.CloseHandle(lpProcessInformation.hThread);
1866 }
1867 if (!flag)
1868 {
1869 string errorMessage = ((num2 == 193 || num2 == 216) ? System.SR.InvalidApplication : GetErrorMessage(num2));
1871 }
1872 }
1873 finally
1874 {
1875 childHandle?.Dispose();
1876 childHandle2?.Dispose();
1877 childHandle3?.Dispose();
1878 }
1879 }
1880 if (startInfo.RedirectStandardInput)
1881 {
1882 Encoding encoding = startInfo.StandardInputEncoding ?? GetEncoding((int)global::Interop.Kernel32.GetConsoleCP());
1883 _standardInput = new StreamWriter(new FileStream(parentHandle, FileAccess.Write, 4096, isAsync: false), encoding, 4096);
1885 }
1886 if (startInfo.RedirectStandardOutput)
1887 {
1888 Encoding encoding2 = startInfo.StandardOutputEncoding ?? GetEncoding((int)global::Interop.Kernel32.GetConsoleOutputCP());
1890 }
1891 if (startInfo.RedirectStandardError)
1892 {
1893 Encoding encoding3 = startInfo.StandardErrorEncoding ?? GetEncoding((int)global::Interop.Kernel32.GetConsoleOutputCP());
1895 }
1896 commandLine.Dispose();
1897 if (safeProcessHandle.IsInvalid)
1898 {
1899 return false;
1900 }
1903 return true;
1904 }
1905
1911
1913 {
1914 ReadOnlySpan<char> value = startInfo.FileName.AsSpan().Trim();
1915 bool flag = value.Length > 0 && value[0] == '"' && value[value.Length - 1] == '"';
1916 if (!flag)
1917 {
1918 commandLine.Append('"');
1919 }
1920 commandLine.Append(value);
1921 if (!flag)
1922 {
1923 commandLine.Append('"');
1924 }
1925 startInfo.AppendArgumentsTo(ref commandLine);
1926 }
1927
1929 {
1931 if (safeProcessHandle.IsInvalid)
1932 {
1934 }
1936 if (!global::Interop.Kernel32.GetProcessTimes(safeProcessHandle, out processThreadTimes._create, out processThreadTimes._exit, out processThreadTimes._kernel, out processThreadTimes._user))
1937 {
1938 throw new Win32Exception();
1939 }
1940 return processThreadTimes;
1941 }
1942
1943 private unsafe static void SetPrivilege(string privilegeName, int attrib)
1944 {
1946 try
1947 {
1948 if (!global::Interop.Advapi32.OpenProcessToken(global::Interop.Kernel32.GetCurrentProcess(), 32, out TokenHandle))
1949 {
1950 throw new Win32Exception();
1951 }
1952 if (!global::Interop.Advapi32.LookupPrivilegeValue(null, privilegeName, out var lpLuid))
1953 {
1954 throw new Win32Exception();
1955 }
1956 Unsafe.SkipInit(out global::Interop.Advapi32.TOKEN_PRIVILEGE tOKEN_PRIVILEGE);
1960 global::Interop.Advapi32.AdjustTokenPrivileges(TokenHandle, DisableAllPrivileges: false, &tOKEN_PRIVILEGE, 0u, null, null);
1961 if (Marshal.GetLastWin32Error() != 0)
1962 {
1963 throw new Win32Exception();
1964 }
1965 }
1966 finally
1967 {
1968 TokenHandle?.Dispose();
1969 }
1970 }
1971
1973 {
1975 {
1976 if (throwIfExited)
1977 {
1978 using global::Interop.Kernel32.ProcessWaitHandle processWaitHandle = new global::Interop.Kernel32.ProcessWaitHandle(_processHandle);
1979 if (processWaitHandle.WaitOne(0))
1980 {
1982 }
1983 }
1985 }
1986 EnsureState((State)3);
1988 if (throwIfExited && ((uint)access & 0x400u) != 0 && global::Interop.Kernel32.GetExitCodeProcess(safeProcessHandle, out _exitCode) && _exitCode != 259)
1989 {
1991 }
1992 return safeProcessHandle;
1993 }
1994
1995 private static void CreatePipeWithSecurityAttributes(out SafeFileHandle hReadPipe, out SafeFileHandle hWritePipe, ref global::Interop.Kernel32.SECURITY_ATTRIBUTES lpPipeAttributes, int nSize)
1996 {
1997 if (!global::Interop.Kernel32.CreatePipe(out hReadPipe, out hWritePipe, ref lpPipeAttributes, nSize) || hReadPipe.IsInvalid || hWritePipe.IsInvalid)
1998 {
1999 throw new Win32Exception();
2000 }
2001 }
2002
2004 {
2005 global::Interop.Kernel32.SECURITY_ATTRIBUTES lpPipeAttributes = default(global::Interop.Kernel32.SECURITY_ATTRIBUTES);
2006 lpPipeAttributes.bInheritHandle = global::Interop.BOOL.TRUE;
2008 try
2009 {
2010 if (parentInputs)
2011 {
2013 }
2014 else
2015 {
2017 }
2018 IntPtr currentProcess = global::Interop.Kernel32.GetCurrentProcess();
2019 if (!global::Interop.Kernel32.DuplicateHandle(currentProcess, (SafeHandle)hReadPipe, currentProcess, out parentHandle, 0, bInheritHandle: false, 2))
2020 {
2021 throw new Win32Exception();
2022 }
2023 }
2024 finally
2025 {
2026 if (hReadPipe != null && !hReadPipe.IsInvalid)
2027 {
2028 hReadPipe.Dispose();
2029 }
2030 }
2031 }
2032
2034 {
2035 string[] array = new string[sd.Count];
2036 sd.Keys.CopyTo(array, 0);
2039 string[] array2 = array;
2040 foreach (string text in array2)
2041 {
2042 stringBuilder.Append(text).Append('=').Append(sd[text])
2043 .Append('\0');
2044 }
2045 return stringBuilder.ToString();
2046 }
2047
2048 private static string GetErrorMessage(int error)
2049 {
2050 return global::Interop.Kernel32.GetMessage(error);
2051 }
2052
2054 {
2055 if (!startInfo.UseShellExecute)
2056 {
2058 }
2060 }
2061
2063 {
2064 if (!string.IsNullOrEmpty(startInfo.UserName) || startInfo.Password != null)
2065 {
2067 }
2068 if (startInfo.RedirectStandardInput || startInfo.RedirectStandardOutput || startInfo.RedirectStandardError)
2069 {
2071 }
2072 if (startInfo.StandardInputEncoding != null)
2073 {
2075 }
2076 if (startInfo.StandardErrorEncoding != null)
2077 {
2079 }
2080 if (startInfo.StandardOutputEncoding != null)
2081 {
2083 }
2084 if (startInfo._environmentVariables != null)
2085 {
2087 }
2088 string text = startInfo.BuildArguments();
2089 fixed (char* lpFile = ((startInfo.FileName.Length > 0) ? startInfo.FileName : null))
2090 {
2091 fixed (char* lpVerb = ((startInfo.Verb.Length > 0) ? startInfo.Verb : null))
2092 {
2093 fixed (char* lpParameters = ((text.Length > 0) ? text : null))
2094 {
2095 fixed (char* lpDirectory = ((startInfo.WorkingDirectory.Length > 0) ? startInfo.WorkingDirectory : null))
2096 {
2097 global::Interop.Shell32.SHELLEXECUTEINFO sHELLEXECUTEINFO = default(global::Interop.Shell32.SHELLEXECUTEINFO);
2098 sHELLEXECUTEINFO.cbSize = (uint)sizeof(global::Interop.Shell32.SHELLEXECUTEINFO);
2099 sHELLEXECUTEINFO.lpFile = lpFile;
2100 sHELLEXECUTEINFO.lpVerb = lpVerb;
2101 sHELLEXECUTEINFO.lpParameters = lpParameters;
2102 sHELLEXECUTEINFO.lpDirectory = lpDirectory;
2104 global::Interop.Shell32.SHELLEXECUTEINFO sHELLEXECUTEINFO2 = sHELLEXECUTEINFO;
2105 if (startInfo.ErrorDialog)
2106 {
2107 sHELLEXECUTEINFO2.hwnd = startInfo.ErrorDialogParentHandle;
2108 }
2109 else
2110 {
2112 }
2114 {
2118 _ => 1,
2119 };
2121 if (!shellExecuteHelper.ShellExecuteOnSTAThread())
2122 {
2123 int num = shellExecuteHelper.ErrorCode;
2124 if (num == 0)
2125 {
2126 num = GetShellError(sHELLEXECUTEINFO2.hInstApp);
2127 }
2128 string text2;
2129 switch (num)
2130 {
2131 case 120:
2133 default:
2134 text2 = GetErrorMessage(num);
2135 break;
2136 case 193:
2137 case 216:
2139 break;
2140 }
2141 string errorMessage = text2;
2142 throw CreateExceptionForErrorStartingProcess(errorMessage, num, startInfo.FileName, startInfo.WorkingDirectory);
2143 }
2144 if (sHELLEXECUTEINFO2.hProcess != IntPtr.Zero)
2145 {
2147 return true;
2148 }
2149 }
2150 }
2151 }
2152 }
2153 return false;
2154 }
2155
2157 {
2158 long num = (long)error;
2159 long num2 = num - 2;
2160 if ((ulong)num2 <= 6uL)
2161 {
2162 switch (num2)
2163 {
2164 case 0L:
2165 return 2;
2166 case 1L:
2167 return 3;
2168 case 3L:
2169 return 5;
2170 case 6L:
2171 return 8;
2172 case 2L:
2173 case 4L:
2174 case 5L:
2175 goto IL_0083;
2176 }
2177 }
2178 long num3 = num - 26;
2179 if ((ulong)num3 <= 6uL)
2180 {
2181 switch (num3)
2182 {
2183 case 2L:
2184 case 3L:
2185 case 4L:
2186 return 1156;
2187 case 0L:
2188 return 32;
2189 case 5L:
2190 return 1155;
2191 case 6L:
2192 return 1157;
2193 }
2194 }
2195 goto IL_0083;
2196 IL_0083:
2197 return (int)(long)error;
2198 }
2199
2200 private unsafe string GetMainWindowTitle()
2201 {
2204 {
2205 return string.Empty;
2206 }
2207 int windowTextLengthW = global::Interop.User32.GetWindowTextLengthW(mainWindowHandle);
2208 if (windowTextLengthW == 0)
2209 {
2210 return string.Empty;
2211 }
2213 Span<char> span = ((windowTextLengthW > 256) ? ((Span<char>)new char[windowTextLengthW]) : stackalloc char[256]);
2215 fixed (char* lpString = span2)
2216 {
2217 windowTextLengthW = global::Interop.User32.GetWindowTextW(mainWindowHandle, lpString, span2.Length);
2218 }
2219 return span2.Slice(0, windowTextLengthW).ToString();
2220 }
2221
2222 private bool CloseMainWindowCore()
2223 {
2225 if (mainWindowHandle == (IntPtr)0)
2226 {
2227 return false;
2228 }
2229 int windowLong = global::Interop.User32.GetWindowLong(mainWindowHandle, -16);
2230 if (((uint)windowLong & 0x8000000u) != 0)
2231 {
2232 return false;
2233 }
2234 global::Interop.User32.PostMessageW(mainWindowHandle, 16, IntPtr.Zero, IntPtr.Zero);
2235 return true;
2236 }
2237
2238 private bool IsRespondingCore()
2239 {
2241 if (mainWindowHandle == (IntPtr)0)
2242 {
2243 return true;
2244 }
2246 return global::Interop.User32.SendMessageTimeout(mainWindowHandle, 0, IntPtr.Zero, IntPtr.Zero, 2, 5000, out pdwResult) != (IntPtr)0;
2247 }
2248
2250 {
2251 using SafeProcessHandle handle = GetProcessHandle(1049600);
2252 return global::Interop.User32.WaitForInputIdle(handle, milliseconds) switch
2253 {
2254 0 => true,
2255 258 => false,
2257 };
2258 }
2259
2261 {
2262 try
2263 {
2264 return StartTime < possibleChild.StartTime && Id == possibleChild.ParentProcessId;
2265 }
2267 {
2268 return false;
2269 }
2270 }
2271
2272 private bool Equals(Process process)
2273 {
2274 try
2275 {
2276 return Id == process.Id && StartTime == process.StartTime;
2277 }
2279 {
2280 return false;
2281 }
2282 }
2283
2285 {
2287 if (safeProcessHandle.IsInvalid)
2288 {
2289 return null;
2290 }
2292 }
2293
2295 {
2296 List<Exception> list = null;
2297 try
2298 {
2299 Kill();
2300 }
2301 catch (Win32Exception item)
2302 {
2303 (list ?? (list = new List<Exception>())).Add(item);
2304 }
2306 try
2307 {
2308 foreach (var item2 in processHandlePairs)
2309 {
2310 List<Exception> list2 = item2.Item1.KillTree(item2.Item2);
2311 if (list2 != null)
2312 {
2313 (list ?? (list = new List<Exception>())).AddRange(list2);
2314 }
2315 }
2316 return list;
2317 }
2318 finally
2319 {
2320 foreach (var item3 in processHandlePairs)
2321 {
2322 item3.Item1.Dispose();
2323 item3.Item2.Dispose();
2324 }
2325 }
2326 }
2327
2329 {
2332 foreach (Process process2 in processes)
2333 {
2335 if (!safeProcessHandle.IsInvalid)
2336 {
2337 if (predicate(this, process2))
2338 {
2340 continue;
2341 }
2342 process2.Dispose();
2343 safeProcessHandle.Dispose();
2344 }
2345 }
2346 return list;
2348 {
2349 try
2350 {
2351 return process.GetProcessHandle(4096, throwIfExited: false);
2352 }
2353 catch (Win32Exception)
2354 {
2356 }
2357 }
2358 }
2359
2360 [UnsupportedOSPlatform("ios")]
2361 [UnsupportedOSPlatform("tvos")]
2362 public void Kill(bool entireProcessTree)
2363 {
2364 if (!entireProcessTree)
2365 {
2366 Kill();
2367 return;
2368 }
2369 EnsureState((State)34);
2371 {
2373 }
2375 if (list == null || list.Count == 0)
2376 {
2377 return;
2378 }
2380 }
2381
2383 {
2385 {
2386 return true;
2387 }
2389 try
2390 {
2392 Process result = this;
2393 do
2394 {
2395 foreach (Process childProcess in result.GetChildProcesses(processes))
2396 {
2397 if (processOfInterest.Equals(childProcess))
2398 {
2399 return true;
2400 }
2401 queue.Enqueue(childProcess);
2402 }
2403 }
2404 while (queue.TryDequeue(out result));
2405 }
2406 finally
2407 {
2409 foreach (Process process in array)
2410 {
2411 process.Dispose();
2412 }
2413 }
2414 return false;
2415 }
2416
2418 {
2419 bool flag = processes == null;
2423 foreach (Process process in array)
2424 {
2425 bool flag2 = flag;
2426 try
2427 {
2428 if (IsParentOf(process))
2429 {
2430 list.Add(process);
2431 flag2 = false;
2432 }
2433 }
2434 finally
2435 {
2436 if (flag2)
2437 {
2438 process.Dispose();
2439 }
2440 }
2441 }
2442 return list;
2443 }
2444
2446 {
2448 {
2449 return e is Win32Exception;
2450 }
2451 return true;
2452 }
2453}
static readonly SafeProcessHandle InvalidHandle
static void Sort(Array array)
Definition Array.cs:2329
void AddRange(IEnumerable< KeyValuePair< TKey, TValue > > collection)
void Add(TKey key, TValue value)
static ? Delegate Remove(Delegate? source, Delegate? value)
Definition Delegate.cs:463
static ? Delegate Combine(Delegate? a, Delegate? b)
Definition Delegate.cs:379
static ProcessModule GetFirstModule(int processId)
readonly List< ThreadInfo > _threadInfoList
Definition ProcessInfo.cs:7
static SafeProcessHandle OpenProcess(int processId, int access, bool throwIfExited)
static ProcessInfo[] GetProcessInfos(string machineName)
static int GetProcessIdFromHandle(SafeProcessHandle processHandle)
static bool IsProcessRunning(int processId)
static IntPtr GetMainWindowHandle(int processId)
static bool IsRemoteMachine(string machineName)
static ProcessModuleCollection GetModules(int processId)
static ProcessInfo GetProcessInfo(int processId, string machineName)
unsafe readonly global::Interop.Shell32.SHELLEXECUTEINFO * _executeInfo
Definition Process.cs:38
unsafe ShellExecuteHelper(global::Interop.Shell32.SHELLEXECUTEINFO *executeInfo)
Definition Process.cs:46
void ErrorReadNotifyUser(string data)
Definition Process.cs:1494
bool Equals(Process process)
Definition Process.cs:2272
static string GetEnvironmentVariablesBlock(IDictionary< string, string > sd)
Definition Process.cs:2033
void SetProcessHandle(SafeProcessHandle processHandle)
Definition Process.cs:1175
TimeSpan PrivilegedProcessorTime
Definition Process.cs:746
static void CreatePipeWithSecurityAttributes(out SafeFileHandle hReadPipe, out SafeFileHandle hWritePipe, ref global::Interop.Kernel32.SECURITY_ATTRIBUTES lpPipeAttributes, int nSize)
Definition Process.cs:1995
static Process Start(string fileName, string arguments)
Definition Process.cs:1231
static unsafe void SetPrivilege(string privilegeName, int attrib)
Definition Process.cs:1943
static Process[] GetProcessesByName(string? processName, string machineName)
Definition Process.cs:1527
static readonly object s_createProcessLock
Definition Process.cs:176
void OutputReadNotifyUser(string data)
Definition Process.cs:1476
bool StartCore(ProcessStartInfo startInfo)
Definition Process.cs:2053
ProcessStartInfo StartInfo
Definition Process.cs:556
bool IsParentOf(Process possibleChild)
Definition Process.cs:2260
void Kill(bool entireProcessTree)
Definition Process.cs:2362
static void BuildCommandLine(ProcessStartInfo startInfo, ref System.Text.ValueStringBuilder commandLine)
Definition Process.cs:1912
unsafe string GetMainWindowTitle()
Definition Process.cs:2200
void SetWorkingSetLimitsCore(IntPtr? newMin, IntPtr? newMax, out IntPtr resultingMin, out IntPtr resultingMax)
Definition Process.cs:1704
async Task WaitForExitAsync(CancellationToken cancellationToken=default(CancellationToken))
Definition Process.cs:1338
SafeProcessHandle _processHandle
Definition Process.cs:98
static Process[] GetProcessesByName(string? processName)
Definition Process.cs:1084
StreamReader _standardError
Definition Process.cs:156
void CreatePipe(out SafeFileHandle parentHandle, out SafeFileHandle childHandle, bool parentInputs)
Definition Process.cs:2003
StreamWriter _standardInput
Definition Process.cs:154
int GetShellError(IntPtr error)
Definition Process.cs:2156
static Process Start(string fileName, IEnumerable< string > arguments)
Definition Process.cs:1238
bool WaitForInputIdle(int milliseconds)
Definition Process.cs:944
List<(Process Process, SafeProcessHandle Handle)> GetProcessHandlePairs(Func< Process, Process, bool > predicate)
Definition Process.cs:2328
ProcessThreadTimes GetProcessTimes()
Definition Process.cs:1928
void GetWorkingSetLimits(out IntPtr minWorkingSet, out IntPtr maxWorkingSet)
Definition Process.cs:1695
SafeProcessHandle GetProcessHandle()
Definition Process.cs:1690
StreamReadMode _outputStreamReadMode
Definition Process.cs:162
ProcessPriorityClass PriorityClassCore
Definition Process.cs:776
static ? Process Start(string fileName, string userName, SecureString password, string domain)
Definition Process.cs:1551
Process(string machineName, bool isRemoteMachine, int processId, ProcessInfo processInfo)
Definition Process.cs:898
StreamReader StandardError
Definition Process.cs:694
void EnsureState(State state)
Definition Process.cs:1006
DataReceivedEventHandler? ErrorDataReceived
Definition Process.cs:873
ProcessPriorityClass _priorityClass
Definition Process.cs:122
StreamReadMode _errorStreamReadMode
Definition Process.cs:164
SafeProcessHandle GetOrOpenProcessHandle()
Definition Process.cs:1165
void SetWorkingSetLimits(IntPtr? min, IntPtr? max)
Definition Process.cs:1064
IReadOnlyList< Process > GetChildProcesses(Process[] processes=null)
Definition Process.cs:2417
static void EnterDebugMode()
Definition Process.cs:1573
SafeProcessHandle GetProcessHandle(int access, bool throwIfExited=true)
Definition Process.cs:1972
bool WaitForExit(int milliseconds)
Definition Process.cs:1328
static Process GetProcessById(int processId)
Definition Process.cs:1079
ProcessThreadCollection Threads
Definition Process.cs:584
StreamWriter StandardInput
Definition Process.cs:659
StreamReader StandardOutput
Definition Process.cs:673
AsyncStreamReader _output
Definition Process.cs:166
static Process GetProcessById(int processId, string machineName)
Definition Process.cs:1070
static Process[] GetProcesses(string machineName)
Definition Process.cs:1094
unsafe bool StartWithCreateProcess(ProcessStartInfo startInfo)
Definition Process.cs:1739
List< Exception > KillTree()
Definition Process.cs:2284
ProcessModule? MainModule
Definition Process.cs:736
bool IsSelfOrDescendantOf(Process processOfInterest)
Definition Process.cs:2382
ISynchronizeInvoke? SynchronizingObject
Definition Process.cs:733
static Process[] GetProcesses()
Definition Process.cs:1089
static Win32Exception CreateExceptionForErrorStartingProcess(string errorMessage, int errorCode, string fileName, string workingDirectory)
Definition Process.cs:1520
static ? Process Start(ProcessStartInfo startInfo)
Definition Process.cs:1258
override void Dispose(bool disposing)
Definition Process.cs:922
unsafe bool StartWithShellExecuteEx(ProcessStartInfo startInfo)
Definition Process.cs:2062
ProcessThreadCollection _threads
Definition Process.cs:106
RegisteredWaitHandle _registeredWaitHandle
Definition Process.cs:148
static Process Start(string fileName)
Definition Process.cs:1224
List< Exception > KillTree(SafeProcessHandle handle)
Definition Process.cs:2294
bool WaitForExitCore(int milliseconds)
Definition Process.cs:1636
AsyncStreamReader _error
Definition Process.cs:168
bool WaitForInputIdleCore(int milliseconds)
Definition Process.cs:2249
static bool IsProcessInvalidException(Exception e)
Definition Process.cs:2445
static void LeaveDebugMode()
Definition Process.cs:1578
ProcessModuleCollection _modules
Definition Process.cs:108
void SetProcessId(int processId)
Definition Process.cs:1185
ProcessPriorityClass PriorityClass
Definition Process.cs:475
DataReceivedEventHandler? OutputDataReceived
Definition Process.cs:871
StreamReader _standardOutput
Definition Process.cs:152
static string GetErrorMessage(int error)
Definition Process.cs:2048
ProcessStartInfo _startInfo
Definition Process.cs:124
static Encoding GetEncoding(int codePage)
Definition Process.cs:1906
static Process GetCurrentProcess()
Definition Process.cs:1107
static ? Process Start(string fileName, string arguments, string userName, SecureString password, string domain)
Definition Process.cs:1563
void CompletionCallback(object waitHandleContext, bool wasSignaled)
Definition Process.cs:910
static int s_cachedSerializationSwitch
Definition Process.cs:174
override string ToString()
Definition Process.cs:1296
static bool IsDefined(Type enumType, object value)
Definition Enum.cs:359
static int ProcessId
static readonly EventArgs Empty
Definition EventArgs.cs:9
static void SuppressFinalize(object obj)
Definition GC.cs:202
Definition GC.cs:8
static string GetCurrentDirectory()
Definition Directory.cs:286
virtual Encoding CurrentEncoding
virtual Stream BaseStream
override void Close()
override void Close()
static unsafe void ZeroFreeGlobalAllocUnicode(IntPtr s)
Definition Marshal.cs:1522
static IntPtr SecureStringToGlobalAllocUnicode(SecureString s)
Definition Marshal.cs:1284
static void InitHandle(SafeHandle safeHandle, IntPtr handle)
Definition Marshal.cs:1562
static void ThrowIfDeserializationInProgress(string switchSuffix, ref int cachedValue)
static string KillEntireProcessTree_DisallowedBecauseTreeContainsCallingProcess
Definition SR.cs:124
static string NoProcessInfo
Definition SR.cs:20
static string CantSetDuplicatePassword
Definition SR.cs:92
static string StandardOutputEncodingNotAllowed
Definition SR.cs:62
static string ProcessHasExited
Definition SR.cs:32
static string InvalidApplication
Definition SR.cs:58
static string MissingProccess
Definition SR.cs:26
static string WaitTillExit
Definition SR.cs:22
static string InputIdleUnkownError
Definition SR.cs:48
static string NoAssociatedProcess
Definition SR.cs:14
static string CantRedirectStreams
Definition SR.cs:74
static string PendingAsyncOperation
Definition SR.cs:76
static string StandardErrorEncodingNotAllowed
Definition SR.cs:64
static string CantGetStandardIn
Definition SR.cs:68
static string ProcessInformationUnavailable
Definition SR.cs:90
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string CantUseEnvVars
Definition SR.cs:116
static string StandardInputEncodingNotAllowed
Definition SR.cs:120
static string CantStartAsUser
Definition SR.cs:114
static string BadMinWorkset
Definition SR.cs:28
static string UseShellExecuteNotSupported
Definition SR.cs:118
static string ProcessHasExitedNoId
Definition SR.cs:34
static string CantGetProcessStartInfo
Definition SR.cs:86
static string CantSetProcessStartInfo
Definition SR.cs:88
static string ArgumentAndArgumentListInitialized
Definition SR.cs:122
static string FileNameMissing
Definition SR.cs:50
static string KillEntireProcessTree_TerminationIncomplete
Definition SR.cs:126
static string CantMixSyncAsyncOperation
Definition SR.cs:72
static string BadMaxWorkset
Definition SR.cs:30
static string CantGetStandardOut
Definition SR.cs:66
static string CantGetStandardError
Definition SR.cs:70
static string NoProcessHandle
Definition SR.cs:24
static string NotSupportedRemote
Definition SR.cs:18
static string ErrorStartingProcess
Definition SR.cs:60
static string NoAsyncOperation
Definition SR.cs:56
static string ProcessIdRequired
Definition SR.cs:16
Definition SR.cs:7
static StringComparer OrdinalIgnoreCase
static Encoding GetSupportedConsoleEncoding(int codepage)
new Task< TResult > WaitAsync(CancellationToken cancellationToken)
Definition Task.cs:231
new TaskAwaiter< TResult > GetAwaiter()
Definition Task.cs:221
static RegisteredWaitHandle RegisterWaitForSingleObject(WaitHandle waitObject, WaitOrTimerCallback callBack, object state, uint millisecondsTimeOutInterval, bool executeOnlyOnce, bool flowExecutionContext)
static Thread CurrentThread
Definition Thread.cs:312
delegate void DataReceivedEventHandler(object sender, DataReceivedEventArgs e)
delegate void ThreadStart()
delegate void EventHandler(object? sender, EventArgs e)
static readonly IntPtr Zero
Definition IntPtr.cs:18