2 // System.Diagnostics.Process.cs
5 // Dick Porter (dick@ximian.com)
7 // (C) 2002 Ximian, Inc.
12 using System.ComponentModel;
13 using System.Runtime.CompilerServices;
14 using System.Runtime.InteropServices;
16 namespace System.Diagnostics {
17 public class Process : Component {
18 [StructLayout(LayoutKind.Sequential)]
19 private struct ProcInfo {
20 public IntPtr process_handle;
21 public IntPtr thread_handle;
26 IntPtr process_handle;
29 /* Private constructor called from other methods */
30 private Process(IntPtr handle, int id) {
31 process_handle=handle;
40 public int BasePriority {
47 public bool EnableRaisingEvents {
55 [MethodImplAttribute(MethodImplOptions.InternalCall)]
56 private extern static int ExitCode_internal(IntPtr handle);
60 return(ExitCode_internal(process_handle));
64 /* Returns the process start time in Windows file
65 * times (ticks from DateTime(1/1/1601 00:00 GMT))
67 [MethodImplAttribute(MethodImplOptions.InternalCall)]
68 private extern static long ExitTime_internal(IntPtr handle);
70 public DateTime ExitTime {
72 return(DateTime.FromFileTime(ExitTime_internal(process_handle)));
76 public IntPtr Handle {
78 return(process_handle);
83 public int HandleCount {
90 public bool HasExited {
103 public string MachineName {
109 public ProcessModule MainModule {
111 return(this.Modules[0]);
116 public IntPtr MainWindowHandle {
123 public string MainWindowTitle {
130 public IntPtr MaxWorkingSet {
139 public IntPtr MinWorkingSet {
147 /* Returns the list of process modules. The main module is
150 [MethodImplAttribute(MethodImplOptions.InternalCall)]
151 private extern ProcessModule[] GetModules_internal();
153 private ProcessModuleCollection module_collection;
155 public ProcessModuleCollection Modules {
157 if(module_collection==null) {
158 module_collection=new ProcessModuleCollection(GetModules_internal());
161 return(module_collection);
166 public int NonpagedSystemMemorySize {
173 public int PagedMemorySize {
180 public int PagedSystemMemorySize {
187 public int PeakPagedMemorySize {
194 public int PeakVirtualMemorySize {
201 public int PeakWorkingSet {
208 public bool PriorityBoostEnabled {
217 public ProcessPriorityClass PriorityClass {
219 return(ProcessPriorityClass.Normal);
226 public int PrivateMemorySize {
233 public TimeSpan PrivilegedProcessorTime {
235 return(new TimeSpan(0));
240 public string ProcessName {
242 return("this-process");
247 public IntPtr ProcessorAffinity {
256 public bool Responding {
262 private StreamReader error_stream=null;
264 public StreamReader StandardError {
266 return(error_stream);
270 private StreamWriter input_stream=null;
272 public StreamWriter StandardInput {
274 return(input_stream);
278 private StreamReader output_stream=null;
280 public StreamReader StandardOutput {
282 return(output_stream);
286 private ProcessStartInfo start_info=null;
288 public ProcessStartInfo StartInfo {
290 if(start_info==null) {
291 start_info=new ProcessStartInfo();
298 throw new ArgumentException("value is null");
305 /* Returns the process start time in Windows file
306 * times (ticks from DateTime(1/1/1601 00:00 GMT))
308 [MethodImplAttribute(MethodImplOptions.InternalCall)]
309 private extern static long StartTime_internal(IntPtr handle);
311 public DateTime StartTime {
313 return(DateTime.FromFileTime(StartTime_internal(process_handle)));
318 public ISynchronizeInvoke SynchronizingObject {
327 public ProcessThreadCollection Threads {
334 public TimeSpan TotalProcessorTime {
336 return(new TimeSpan(0));
341 public TimeSpan UserProcessorTime {
343 return(new TimeSpan(0));
348 public int VirtualMemorySize {
355 public int WorkingSet {
362 public void Close() {
366 public bool CloseMainWindow() {
371 public static void EnterDebugMode() {
374 [MethodImplAttribute(MethodImplOptions.InternalCall)]
375 private extern static IntPtr GetCurrentProcess_internal();
377 [MethodImplAttribute(MethodImplOptions.InternalCall)]
378 private extern static int GetPid_internal();
380 public static Process GetCurrentProcess() {
381 return(new Process(GetCurrentProcess_internal(),
386 public static Process GetProcessById(int processId) {
391 public static Process GetProcessById(int processId, string machineName) {
396 public static Process[] GetProcesses() {
401 public static Process[] GetProcesses(string machineName) {
406 public static Process[] GetProcessesByName(string processName) {
411 public static Process[] GetProcessesByName(string processName, string machineName) {
420 public static void LeaveDebugMode() {
424 public void Refresh() {
427 [MethodImplAttribute(MethodImplOptions.InternalCall)]
428 private extern static bool Start_internal(string file,
433 ref ProcInfo proc_info);
435 private static bool Start_common(ProcessStartInfo startInfo,
437 ProcInfo proc_info=new ProcInfo();
438 IntPtr stdin_rd, stdin_wr;
439 IntPtr stdout_rd, stdout_wr;
440 IntPtr stderr_rd, stderr_wr;
443 if(startInfo.RedirectStandardInput==true) {
444 ret=MonoIO.CreatePipe(out stdin_rd,
447 stdin_rd=MonoIO.ConsoleInput;
448 /* This is required to stop the
449 * &$*£ing stupid compiler moaning
450 * that stdin_wr is unassigned, below.
455 if(startInfo.RedirectStandardOutput==true) {
456 ret=MonoIO.CreatePipe(out stdout_rd,
460 stdout_wr=MonoIO.ConsoleOutput;
463 if(startInfo.RedirectStandardError==true) {
464 ret=MonoIO.CreatePipe(out stderr_rd,
468 stderr_wr=MonoIO.ConsoleError;
471 ret=Start_internal(startInfo.FileName,
473 stdin_rd, stdout_wr, stderr_wr,
476 process.process_handle=proc_info.process_handle;
477 process.pid=proc_info.pid;
479 if(startInfo.RedirectStandardInput==true) {
480 MonoIO.Close(stdin_rd);
481 process.input_stream=new StreamWriter(new FileStream(stdin_wr, FileAccess.Write, true));
484 if(startInfo.RedirectStandardOutput==true) {
485 MonoIO.Close(stdout_wr);
486 process.output_stream=new StreamReader(new FileStream(stdout_rd, FileAccess.Read, true));
489 if(startInfo.RedirectStandardError==true) {
490 MonoIO.Close(stderr_wr);
491 process.error_stream=new StreamReader(new FileStream(stderr_rd, FileAccess.Read, true));
497 public bool Start() {
500 ret=Start_common(start_info, this);
505 public static Process Start(ProcessStartInfo startInfo) {
506 Process process=new Process();
509 ret=Start_common(startInfo, process);
519 public static Process Start(string fileName) {
524 public static Process Start(string fileName, string arguments) {
529 public override string ToString() {
530 return("process name");
533 /* Waits up to ms milliseconds for process 'handle' to
534 * exit. ms can be <0 to mean wait forever.
536 [MethodImplAttribute(MethodImplOptions.InternalCall)]
537 private extern bool WaitForExit_internal(IntPtr handle,
540 public void WaitForExit() {
541 WaitForExit_internal(process_handle, -1);
544 public bool WaitForExit(int milliseconds) {
545 return(WaitForExit_internal(process_handle,
550 public bool WaitForInputIdle() {
555 public bool WaitForInputIdle(int milliseconds) {
560 public event EventHandler Exited;
562 // Closes the system process handle
563 [MethodImplAttribute(MethodImplOptions.InternalCall)]
564 private extern void Process_free_internal(IntPtr handle);
566 private bool disposed = false;
568 protected override void Dispose(bool disposing) {
569 // Check to see if Dispose has already been called.
571 // If this is a call to Dispose,
572 // dispose all managed resources.
577 // Release unmanaged resources
581 if(process_handle!=IntPtr.Zero) {
583 Process_free_internal(process_handle);
584 process_handle=IntPtr.Zero;
590 public void Dispose() {
592 // Take yourself off the Finalization queue
593 GC.SuppressFinalize(this);
597 protected void OnExited() {