~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/monomac/samples/macdoc/ProcessUtils.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
Import upstream version 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// ProcessUtils.cs
 
3
//
 
4
// Author:
 
5
//       Jérémie Laval <jeremie.laval@xamarin.com>
 
6
//
 
7
// Copyright (c) 2012 Xamarin, Inc.
 
8
 
 
9
using System;
 
10
using System.IO;
 
11
using System.Diagnostics;
 
12
using System.Threading;
 
13
using System.Threading.Tasks;
 
14
 
 
15
namespace macdoc
 
16
{
 
17
        public static class ProcessUtils
 
18
        {
 
19
                public static Task<int> StartProcess (ProcessStartInfo psi, TextWriter stdout, TextWriter stderr, CancellationToken cancellationToken)
 
20
                {
 
21
                        var tcs = new TaskCompletionSource<int> ();
 
22
                        if (cancellationToken.CanBeCanceled && cancellationToken.IsCancellationRequested) {
 
23
                                tcs.SetCanceled ();
 
24
                                return tcs.Task;
 
25
                        }
 
26
 
 
27
                        psi.UseShellExecute = false;
 
28
                        if (stdout != null) {
 
29
                                psi.RedirectStandardOutput = true;
 
30
                        }
 
31
                        if (stderr != null) {
 
32
                                psi.RedirectStandardError = true;
 
33
                        }
 
34
                        var p = Process.Start (psi);
 
35
                        if (cancellationToken.CanBeCanceled)
 
36
                                cancellationToken.Register (() => {
 
37
                                        try {
 
38
                                                if (!p.HasExited) {
 
39
                                                        p.Kill ();
 
40
                                                }
 
41
                                        } catch (InvalidOperationException ex) {
 
42
                                                if (ex.Message.IndexOf ("already exited") < 0)
 
43
                                                        throw;
 
44
                                        }
 
45
                                });
 
46
                        p.EnableRaisingEvents = true;
 
47
                        if (psi.RedirectStandardOutput) {
 
48
                                bool stdOutInitialized = false;
 
49
                                p.OutputDataReceived += (sender, e) => {
 
50
                                        try {
 
51
                                                if (stdOutInitialized)
 
52
                                                        stdout.WriteLine ();
 
53
                                                stdout.Write (e.Data);
 
54
                                                stdOutInitialized = true;
 
55
                                        } catch (Exception ex) {
 
56
                                                tcs.SetException (ex);
 
57
                                        }
 
58
                                };
 
59
                                p.BeginOutputReadLine ();
 
60
                        }
 
61
                        if (psi.RedirectStandardError) {
 
62
                                bool stdErrInitialized = false;
 
63
                                p.ErrorDataReceived += (sender, e) => {
 
64
                                        try {
 
65
                                                if (stdErrInitialized)
 
66
                                                        stderr.WriteLine ();
 
67
                                                stderr.Write (e.Data);
 
68
                                                stdErrInitialized = true;
 
69
                                        } catch (Exception ex) {
 
70
                                                tcs.SetException (ex);
 
71
                                        }
 
72
                                };
 
73
                                p.BeginErrorReadLine ();
 
74
                        }
 
75
                        p.Exited += (sender, e) => tcs.SetResult (p.ExitCode);
 
76
 
 
77
                        return tcs.Task;
 
78
                }
 
79
 
 
80
                public static void StartRelaunchProcess ()
 
81
                {
 
82
 
 
83
                }
 
84
        }
 
85
}
 
86