[mscorlib][System] Use PlatformNotSupportedException instead of NotSupportedException...
[mono.git] / mcs / class / System / System.Diagnostics / Process.cs
index d9888b7e40385880e5d3210a6276348d42f2abd3..b46bc936c1df2df53242753e43da03d6a2b0aa72 100644 (file)
@@ -80,8 +80,6 @@ namespace System.Diagnostics {
                Thread background_wait_for_exit_thread;
                ISynchronizeInvoke synchronizingObject;
                EventHandler exited_event;
-               IntPtr stdout_rd;
-               IntPtr stderr_rd;
 
                /* Private constructor called from other methods */
                private Process(IntPtr handle, int id) {
@@ -579,6 +577,7 @@ namespace System.Diagnostics {
                        }
                }
 
+#if MONO_FEATURE_PROCESS_START
                private StreamReader error_stream=null;
                bool error_stream_exposed;
 
@@ -650,6 +649,28 @@ namespace System.Diagnostics {
                                start_info = value;
                        }
                }
+#else
+               [Obsolete ("Process.StandardError is not supported on the current platform.", true)]
+               public StreamReader StandardError {
+                       get { throw new PlatformNotSupportedException ("Process.StandardError is not supported on the current platform."); }
+               }
+
+               [Obsolete ("Process.StandardInput is not supported on the current platform.", true)]
+               public StreamWriter StandardInput {
+                       get { throw new PlatformNotSupportedException ("Process.StandardInput is not supported on the current platform."); }
+               }
+
+               [Obsolete ("Process.StandardOutput is not supported on the current platform.", true)]
+               public StreamReader StandardOutput {
+                       get { throw new PlatformNotSupportedException ("Process.StandardOutput is not supported on the current platform."); }
+               }
+
+               [Obsolete ("Process.StartInfo is not supported on the current platform.", true)]
+               public ProcessStartInfo StartInfo {
+                       get { throw new PlatformNotSupportedException ("Process.StartInfo is not supported on the current platform."); }
+                       set { throw new PlatformNotSupportedException ("Process.StartInfo is not supported on the current platform."); }
+               }
+#endif // MONO_FEATURE_PROCESS_START
 
                /* Returns the process start time in Windows file
                 * times (ticks from DateTime(1/1/1601 00:00 GMT))
@@ -897,6 +918,7 @@ namespace System.Diagnostics {
                        // the process (currently we have none).
                }
 
+#if MONO_FEATURE_PROCESS_START
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                private extern static bool ShellExecuteEx_internal(ProcessStartInfo startInfo,
                                                                   ref ProcInfo proc_info);
@@ -1019,17 +1041,15 @@ namespace System.Diagnostics {
 
                                if (startInfo.RedirectStandardOutput) {
                                        CreatePipe (out stdout_read, out stdout_write, false);
-                                       process.stdout_rd = stdout_read;
                                } else {
-                                       process.stdout_rd = IntPtr.Zero;
+                                       stdout_read = IntPtr.Zero;
                                        stdout_write = MonoIO.ConsoleOutput;
                                }
 
                                if (startInfo.RedirectStandardError) {
                                        CreatePipe (out stderr_read, out stderr_write, false);
-                                       process.stderr_rd  = stderr_read;
                                } else {
-                                       process.stderr_rd = IntPtr.Zero;
+                                       stderr_read = IntPtr.Zero;
                                        stderr_write = MonoIO.ConsoleError;
                                }
 
@@ -1092,7 +1112,7 @@ namespace System.Diagnostics {
 #else
                                var stdinEncoding = Console.InputEncoding;
 #endif
-                               process.input_stream = new StreamWriter (new FileStream (new SafeFileHandle (stdin_write, false), FileAccess.Write, 8192, false), stdinEncoding) {
+                               process.input_stream = new StreamWriter (new FileStream (stdin_write, FileAccess.Write, true, 8192), stdinEncoding) {
                                        AutoFlush = true
                                };
                        }
@@ -1102,7 +1122,7 @@ namespace System.Diagnostics {
 
                                Encoding stdoutEncoding = startInfo.StandardOutputEncoding ?? Console.Out.Encoding;
 
-                               process.output_stream = new StreamReader (new FileStream (new SafeFileHandle (stdout_read, false), FileAccess.Read, 8192, false), stdoutEncoding, true, 8192);
+                               process.output_stream = new StreamReader (new FileStream (stdout_read, FileAccess.Read, true, 8192), stdoutEncoding, true);
                        }
 
                        if (startInfo.RedirectStandardError) {
@@ -1110,7 +1130,7 @@ namespace System.Diagnostics {
 
                                Encoding stderrEncoding = startInfo.StandardErrorEncoding ?? Console.Out.Encoding;
 
-                               process.error_stream = new StreamReader (new FileStream (new SafeFileHandle (stderr_read, false), FileAccess.Read, 8192, false), stderrEncoding, true, 8192);
+                               process.error_stream = new StreamReader (new FileStream (stderr_read, FileAccess.Read, true, 8192), stderrEncoding, true);
                        }
 
                        process.StartBackgroundWaitForExit ();
@@ -1195,6 +1215,43 @@ namespace System.Diagnostics {
                        psi.UseShellExecute = false;
                        return Start(psi);
                }
+#else
+               [Obsolete ("Process.Start is not supported on the current platform.", true)]
+               public bool Start ()
+               {
+                       throw new PlatformNotSupportedException ("Process.Start is not supported on the current platform.");
+               }
+
+               [Obsolete ("Process.Start is not supported on the current platform.", true)]
+               public static Process Start (ProcessStartInfo startInfo)
+               {
+                       throw new PlatformNotSupportedException ("Process.Start is not supported on the current platform.");
+               }
+
+               [Obsolete ("Process.Start is not supported on the current platform.", true)]
+               public static Process Start (string fileName)
+               {
+                       throw new PlatformNotSupportedException ("Process.Start is not supported on the current platform.");
+               }
+
+               [Obsolete ("Process.Start is not supported on the current platform.", true)]
+               public static Process Start(string fileName, string arguments)
+               {
+                       throw new PlatformNotSupportedException ("Process.Start is not supported on the current platform.");
+               }
+
+               [Obsolete ("Process.Start is not supported on the current platform.", true)]
+               public static Process Start(string fileName, string username, SecureString password, string domain)
+               {
+                       throw new PlatformNotSupportedException ("Process.Start is not supported on the current platform.");
+               }
+
+               [Obsolete ("Process.Start is not supported on the current platform.", true)]
+               public static Process Start(string fileName, string arguments, string username, SecureString password, string domain)
+               {
+                       throw new PlatformNotSupportedException ("Process.Start is not supported on the current platform.");
+               }
+#endif // MONO_FEATURE_PROCESS_START
 
                public override string ToString()
                {
@@ -1220,38 +1277,16 @@ namespace System.Diagnostics {
                        if (process_handle == IntPtr.Zero)
                                throw new InvalidOperationException ("No process is associated with this object.");
 
-                       DateTime start = DateTime.UtcNow;
-
                        if (!WaitForExit_internal (process_handle, ms))
                                return false;
 
-                       if (ms >= 0) {
-                               ms -= (int) (DateTime.UtcNow - start).TotalMilliseconds;
-                               if (ms <= 0)
-                                       return false;
-                       }
-
-                       if (async_output != null && !async_output.IsCompleted) {
-                               if (false == async_output.AsyncWaitHandle.WaitOne (ms, false))
-                                       return false; // Timed out
+#if MONO_FEATURE_PROCESS_START
+                       if (async_output != null && !async_output.IsCompleted)
+                               async_output.AsyncWaitHandle.WaitOne ();
 
-                               if (ms >= 0) {
-                                       ms -= (int) (DateTime.UtcNow - start).TotalMilliseconds;
-                                       if (ms <= 0)
-                                               return false;
-                               }
-                       }
-
-                       if (async_error != null && !async_error.IsCompleted) {
-                               if (false == async_error.AsyncWaitHandle.WaitOne (ms, false))
-                                       return false; // Timed out
-
-                               if (ms >= 0) {
-                                       ms -= (int) (DateTime.UtcNow - start).TotalMilliseconds;
-                                       if (ms <= 0)
-                                               return false;
-                               }
-                       }
+                       if (async_error != null && !async_error.IsCompleted)
+                               async_error.AsyncWaitHandle.WaitOne ();
+#endif // MONO_FEATURE_PROCESS_START
 
                        OnExited ();
 
@@ -1305,6 +1340,7 @@ namespace System.Diagnostics {
                                cb (this, new DataReceivedEventArgs (str));
                }
 
+#if MONO_FEATURE_PROCESS_START
                [Flags]
                enum AsyncModes {
                        NoneYet = 0,
@@ -1325,27 +1361,32 @@ namespace System.Diagnostics {
                        StringBuilder sb = new StringBuilder ();
                        byte[] buffer = new byte [4096];
 
-                       public ProcessAsyncReader (Process process, IntPtr handle, bool err_out)
+                       const int ERROR_INVALID_HANDLE = 6;
+
+                       public ProcessAsyncReader (Process process, FileStream stream, bool err_out)
                                : base (null, null)
                        {
                                this.process = process;
-                               this.handle = handle;
-                               this.stream = new FileStream (handle, FileAccess.Read, false);
+                               this.handle = stream.SafeFileHandle.DangerousGetHandle ();
+                               this.stream = stream;
                                this.err_out = err_out;
                        }
 
-                       public void BeginRead ()
+                       public void BeginReadLine ()
                        {
-                               IOSelector.Add (this.handle, new IOSelectorJob (IOOperation.Read, _ => AddInput (), null));
+                               IOSelector.Add (this.handle, new IOSelectorJob (IOOperation.Read, _ => Read (), null));
                        }
 
-                       public void AddInput ()
+                       void Read ()
                        {
                                int nread = 0;
 
                                try {
                                        nread = stream.Read (buffer, 0, buffer.Length);
                                } catch (ObjectDisposedException) {
+                               } catch (IOException ex) {
+                                       if (ex.HResult != (unchecked((int) 0x80070000) | (int) ERROR_INVALID_HANDLE))
+                                               throw;
                                } catch (NotSupportedException) {
                                        if (stream.CanRead)
                                                throw;
@@ -1375,7 +1416,7 @@ namespace System.Diagnostics {
 
                                Flush (false);
 
-                               IOSelector.Add (this.handle, new IOSelectorJob (IOOperation.Read, _ => AddInput (), null));
+                               IOSelector.Add (this.handle, new IOSelectorJob (IOOperation.Read, _ => Read (), null));
                        }
 
                        void Flush (bool last)
@@ -1411,7 +1452,6 @@ namespace System.Diagnostics {
                        public void Close ()
                        {
                                IOSelector.Remove (handle);
-                               stream.Close ();
                        }
 
                        internal override void CompleteDisposed ()
@@ -1438,8 +1478,8 @@ namespace System.Diagnostics {
                        async_mode |= AsyncModes.AsyncOutput;
                        output_canceled = false;
                        if (async_output == null) {
-                               async_output = new ProcessAsyncReader (this, stdout_rd, true);
-                               async_output.BeginRead ();
+                               async_output = new ProcessAsyncReader (this, (FileStream) output_stream.BaseStream, true);
+                               async_output.BeginReadLine ();
                        }
                }
 
@@ -1470,8 +1510,8 @@ namespace System.Diagnostics {
                        async_mode |= AsyncModes.AsyncError;
                        error_canceled = false;
                        if (async_error == null) {
-                               async_error = new ProcessAsyncReader (this, stderr_rd, false);
-                               async_error.BeginRead ();
+                               async_error = new ProcessAsyncReader (this, (FileStream) error_stream.BaseStream, false);
+                               async_error.BeginReadLine ();
                        }
                }
 
@@ -1489,6 +1529,31 @@ namespace System.Diagnostics {
 
                        error_canceled = true;
                }
+#else
+               [Obsolete ("Process.BeginOutputReadLine is not supported on the current platform.", true)]
+               public void BeginOutputReadLine ()
+               {
+                       throw new PlatformNotSupportedException ("Process.BeginOutputReadLine is not supported on the current platform.");
+               }
+
+               [Obsolete ("Process.BeginOutputReadLine is not supported on the current platform.", true)]
+               public void CancelOutputRead ()
+               {
+                       throw new PlatformNotSupportedException ("Process.BeginOutputReadLine is not supported on the current platform.");
+               }
+
+               [Obsolete ("Process.BeginOutputReadLine is not supported on the current platform.", true)]
+               public void BeginErrorReadLine ()
+               {
+                       throw new PlatformNotSupportedException ("Process.BeginOutputReadLine is not supported on the current platform.");
+               }
+
+               [Obsolete ("Process.BeginOutputReadLine is not supported on the current platform.", true)]
+               public void CancelErrorRead ()
+               {
+                       throw new PlatformNotSupportedException ("Process.BeginOutputReadLine is not supported on the current platform.");
+               }
+#endif // MONO_FEATURE_PROCESS_START
 
                [Category ("Behavior")]
                [MonitoringDescription ("Raised when this process exits.")]
@@ -1521,6 +1586,7 @@ namespace System.Diagnostics {
                        // If this is a call to Dispose,
                        // dispose all managed resources.
                        if (disposing) {
+#if MONO_FEATURE_PROCESS_START
                                /* These have open FileStreams on the pipes we are about to close */
                                if (async_output != null)
                                        async_output.Close ();
@@ -1542,6 +1608,7 @@ namespace System.Diagnostics {
                                                error_stream.Close ();
                                        error_stream = null;
                                }
+#endif // MONO_FEATURE_PROCESS_START
                        }
 
                        // Release unmanaged resources
@@ -1608,7 +1675,7 @@ namespace System.Diagnostics {
                        if (background_wait_for_exit_thread != null)
                                return;
 
-                       Thread t = new Thread (_ => WaitForExit ());
+                       Thread t = new Thread (_ => WaitForExit ()) { IsBackground = true };
 
                        if (Interlocked.CompareExchange (ref background_wait_for_exit_thread, t, null) == null)
                                t.Start ();