Merge pull request #963 from kebby/master
[mono.git] / mcs / class / System / System.Diagnostics / Process.cs
index d22a75dd76e03dc959226457663b181d15ecc715..af427037b285cfc9ae8e1b4e7aa7183d1fddc04b 100644 (file)
@@ -102,7 +102,6 @@ namespace System.Diagnostics {
 
                void StartExitCallbackIfNeeded ()
                {
-#if !NET_2_1
                        bool start = (!already_waiting && enableRaisingEvents && exited_event != null);
                        if (start && process_handle != IntPtr.Zero) {
                                WaitOrTimerCallback cb = new WaitOrTimerCallback (CBOnExit);
@@ -110,7 +109,6 @@ namespace System.Diagnostics {
                                ThreadPool.RegisterWaitForSingleObject (h, cb, this, -1, true);
                                already_waiting = true;
                        }
-#endif
                }
 
                [DefaultValue (false), Browsable (false)]
@@ -685,11 +683,13 @@ namespace System.Diagnostics {
                }
 
                [MonoTODO]
-               [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden), Browsable (false)]
+               [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
                [MonitoringDescription ("The number of threads of this process.")]
                public ProcessThreadCollection Threads {
                        get {
-                               return ProcessThreadCollection.GetEmpty ();
+                               // This'll return a correctly-sized array of empty ProcessThreads for now.
+                               int error;
+                               return new ProcessThreadCollection(new ProcessThread[GetProcessData (pid, 0, out error)]);
                        }
                }
 
@@ -918,8 +918,7 @@ namespace System.Diagnostics {
                                                                  IntPtr stderr,
                                                                  ref ProcInfo proc_info);
 
-               private static bool Start_shell (ProcessStartInfo startInfo,
-                                                Process process)
+               private static bool Start_shell (ProcessStartInfo startInfo, Process process)
                {
                        ProcInfo proc_info=new ProcInfo();
                        bool ret;
@@ -948,9 +947,7 @@ namespace System.Diagnostics {
 
                        process.process_handle = proc_info.process_handle;
                        process.pid = proc_info.pid;
-
                        process.StartExitCallbackIfNeeded ();
-
                        return(ret);
                }
 
@@ -1182,9 +1179,9 @@ namespace System.Diagnostics {
                        if (startInfo == null)
                                throw new ArgumentNullException ("startInfo");
 
-                       Process process=new Process();
+                       Process process = new Process();
                        process.StartInfo = startInfo;
-                       if (Start_common(startInfo, process))
+                       if (Start_common(startInfo, process) && process.process_handle != IntPtr.Zero)
                                return process;
                        return null;
                }
@@ -1517,8 +1514,8 @@ namespace System.Diagnostics {
                [ComVisibleAttribute(false)] 
                public void CancelErrorRead ()
                {
-                       if (process_handle == IntPtr.Zero || output_stream == null || StartInfo.RedirectStandardOutput == false)
-                               throw new InvalidOperationException ("Standard output has not been redirected or process has not been started.");
+                       if (process_handle == IntPtr.Zero || error_stream == null || StartInfo.RedirectStandardError == false)
+                               throw new InvalidOperationException ("Standard error has not been redirected or process has not been started.");
 
                        if ((async_mode & AsyncModes.SyncOutput) != 0)
                                throw new InvalidOperationException ("OutputStream is not enabled for asynchronous read operations.");
@@ -1566,6 +1563,21 @@ namespace System.Diagnostics {
                                                        async_output.Close ();
                                                if (async_error != null)
                                                        async_error.Close ();
+
+                                               if (input_stream != null) {
+                                                       input_stream.Close();
+                                                       input_stream = null;
+                                               }
+
+                                               if (output_stream != null) {
+                                                       output_stream.Close();
+                                                       output_stream = null;
+                                               }
+
+                                               if (error_stream != null) {
+                                                       error_stream.Close();
+                                                       error_stream = null;
+                                               }
                                        }
                                }
                                
@@ -1576,21 +1588,6 @@ namespace System.Diagnostics {
                                                Process_free_internal(process_handle);
                                                process_handle=IntPtr.Zero;
                                        }
-
-                                       if (input_stream != null) {
-                                               input_stream.Close();
-                                               input_stream = null;
-                                       }
-
-                                       if (output_stream != null) {
-                                               output_stream.Close();
-                                               output_stream = null;
-                                       }
-
-                                       if (error_stream != null) {
-                                               error_stream.Close();
-                                               error_stream = null;
-                                       }
                                }
                        }
                        base.Dispose (disposing);