Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
ShellHelper.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System.Collections.Generic;
4 using System.Diagnostics;
5 
6 namespace SiliconStudio
7 {
8  public partial class ShellHelper
9  {
10  /// <summary>
11  /// Run the process and get the output without deadlocks.
12  /// </summary>
13  /// <param name="command">The name or path of the command.</param>
14  /// <param name="parameters">The parameters of the command.</param>
15  /// <returns>The outputs.</returns>
16  public static ProcessOutputs RunProcessAndGetOutput(string command, string parameters)
17  {
18  var outputs = new ProcessOutputs();
19  var adbProcess = Process.Start(
20  new ProcessStartInfo(command, parameters)
21  {
22  UseShellExecute = false,
23  CreateNoWindow = true,
24  RedirectStandardError = true,
25  RedirectStandardOutput = true,
26  });
27  adbProcess.OutputDataReceived += (_, args) => LockProcessAndAddDataToList(adbProcess, outputs.OutputLines, args);
28  adbProcess.ErrorDataReceived += (_, args) => LockProcessAndAddDataToList(adbProcess, outputs.OutputErrors, args);
29  adbProcess.BeginOutputReadLine();
30  adbProcess.BeginErrorReadLine();
31  adbProcess.WaitForExit();
32 
33  outputs.ExitCode = adbProcess.ExitCode;
34 
35  return outputs;
36  }
37 
38  /// <summary>
39  /// Run a process without waiting for its output.
40  /// </summary>
41  /// <param name="command">The name or path of the command.</param>
42  /// <param name="parameters">The parameters of the command.</param>
43  public static Process RunProcess(string command, string parameters)
44  {
45  return Process.Start(
46  new ProcessStartInfo(command, parameters)
47  {
48  UseShellExecute = false,
49  CreateNoWindow = true,
50  RedirectStandardError = true,
51  RedirectStandardOutput = true,
52  });
53  }
54 
55  /// <summary>
56  /// Lock the process and save the string.
57  /// </summary>
58  /// <param name="process">The current process.</param>
59  /// <param name="output">List of saved strings.</param>
60  /// <param name="args">arguments of the process.</param>
61  private static void LockProcessAndAddDataToList(Process process, List<string> output, DataReceivedEventArgs args)
62  {
63  if (!string.IsNullOrEmpty(args.Data))
64  {
65  lock (process)
66  {
67  output.Add(args.Data);
68  }
69  }
70  }
71  }
72 }
static ProcessOutputs RunProcessAndGetOutput(string command, string parameters)
Run the process and get the output without deadlocks.
Definition: ShellHelper.cs:16
static Process RunProcess(string command, string parameters)
Run a process without waiting for its output.
Definition: ShellHelper.cs:43