diff options
author | Jeremie Laval <jeremie.laval@gmail.com> | 2012-11-20 22:11:42 +0400 |
---|---|---|
committer | Jeremie Laval <jeremie.laval@gmail.com> | 2012-11-20 22:11:42 +0400 |
commit | 29e95b76d844c76cd3ccffc991e57604339c39ee (patch) | |
tree | bfffe2f8223880bc11ec23c90a7d536df6d6529f /ProcessUtils.cs | |
parent | e798cd51f95f2ea0c1982bb55edbed8396f07186 (diff) |
[macdoc] Update to use the new apple-doc-wizard setuid shim (no password required anymore except to fix the binary). We also now show a sort-of progress window that allow to restart macdoc after the merge to pick up the new changes.
Diffstat (limited to 'ProcessUtils.cs')
-rw-r--r-- | ProcessUtils.cs | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/ProcessUtils.cs b/ProcessUtils.cs new file mode 100644 index 0000000..39906bd --- /dev/null +++ b/ProcessUtils.cs @@ -0,0 +1,86 @@ +// +// ProcessUtils.cs +// +// Author: +// Jérémie Laval <jeremie.laval@xamarin.com> +// +// Copyright (c) 2012 Xamarin, Inc. + +using System; +using System.IO; +using System.Diagnostics; +using System.Threading; +using System.Threading.Tasks; + +namespace macdoc +{ + public static class ProcessUtils + { + public static Task<int> StartProcess (ProcessStartInfo psi, TextWriter stdout, TextWriter stderr, CancellationToken cancellationToken) + { + var tcs = new TaskCompletionSource<int> (); + if (cancellationToken.CanBeCanceled && cancellationToken.IsCancellationRequested) { + tcs.SetCanceled (); + return tcs.Task; + } + + psi.UseShellExecute = false; + if (stdout != null) { + psi.RedirectStandardOutput = true; + } + if (stderr != null) { + psi.RedirectStandardError = true; + } + var p = Process.Start (psi); + if (cancellationToken.CanBeCanceled) + cancellationToken.Register (() => { + try { + if (!p.HasExited) { + p.Kill (); + } + } catch (InvalidOperationException ex) { + if (ex.Message.IndexOf ("already exited") < 0) + throw; + } + }); + p.EnableRaisingEvents = true; + if (psi.RedirectStandardOutput) { + bool stdOutInitialized = false; + p.OutputDataReceived += (sender, e) => { + try { + if (stdOutInitialized) + stdout.WriteLine (); + stdout.Write (e.Data); + stdOutInitialized = true; + } catch (Exception ex) { + tcs.SetException (ex); + } + }; + p.BeginOutputReadLine (); + } + if (psi.RedirectStandardError) { + bool stdErrInitialized = false; + p.ErrorDataReceived += (sender, e) => { + try { + if (stdErrInitialized) + stderr.WriteLine (); + stderr.Write (e.Data); + stdErrInitialized = true; + } catch (Exception ex) { + tcs.SetException (ex); + } + }; + p.BeginErrorReadLine (); + } + p.Exited += (sender, e) => tcs.SetResult (p.ExitCode); + + return tcs.Task; + } + + public static void StartRelaunchProcess () + { + + } + } +} + |