Skip to content

Commit 58ace37

Browse files
committed
Fixes to ProcessHelper
1 parent b9a9295 commit 58ace37

File tree

2 files changed

+28
-14
lines changed

2 files changed

+28
-14
lines changed

QtSharp.CLI/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ static bool QueryQt(QtInfo qt, bool debug)
129129

130130
string emptyFile = Platform.IsWindows ? "NUL" : "/dev/null";
131131
string output;
132-
ProcessHelper.Run("gcc", string.Format("-v -E -x c++ {0}", emptyFile), out output, waitForExit: !Platform.IsWindows);
132+
ProcessHelper.Run("gcc", string.Format("-v -E -x c++ {0}", emptyFile), out output);
133133
qt.Target = Regex.Match(output, @"Target:\s*(?<target>[^\r\n]+)").Groups["target"].Value;
134134

135135
const string includeDirsRegex = @"#include <\.\.\.> search starts here:(?<includes>.+)End of search list";

QtSharp/ProcessHelper.cs

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,46 @@ namespace QtSharp
55
{
66
public class ProcessHelper
77
{
8-
public static string Run(string path, string args, out string error, bool readOutputByLines = false, bool waitForExit = true)
8+
public static string Run(string path, string args, out string error, bool readOutputByLines = false)
99
{
1010
try
1111
{
1212
using (Process process = new Process())
1313
{
14+
Console.WriteLine("Run: " + path);
15+
Console.WriteLine("Args: " + args);
1416
process.StartInfo.FileName = path;
1517
process.StartInfo.Arguments = args;
1618
process.StartInfo.UseShellExecute = false;
1719
process.StartInfo.RedirectStandardOutput = true;
1820
process.StartInfo.RedirectStandardError = true;
19-
process.Start();
20-
if (waitForExit)
21+
22+
var reterror = "";
23+
var retout = "";
24+
process.OutputDataReceived += (sender, outargs) =>
2125
{
22-
process.WaitForExit();
23-
}
24-
while (readOutputByLines && !process.StandardOutput.EndOfStream)
26+
if (retout != "" && outargs.Data != "") retout += "\r\n";
27+
retout += outargs.Data;
28+
Console.WriteLine("stdout: {0}", retout);
29+
};
30+
process.ErrorDataReceived += (sender, errargs) =>
2531
{
26-
Console.WriteLine(process.StandardOutput.ReadLine());
27-
}
28-
error = process.StandardError.ReadToEnd();
32+
if (reterror != "" && errargs.Data != "") reterror += "\r\n";
33+
reterror += errargs.Data;
34+
Console.WriteLine("stderr: {0}", reterror);
35+
};
36+
37+
process.Start();
38+
process.BeginOutputReadLine();
39+
process.BeginErrorReadLine();
40+
process.WaitForExit();
41+
process.CancelOutputRead();
42+
process.CancelErrorRead();
43+
44+
error = reterror;
2945
if (process.ExitCode != 0)
30-
{
31-
return string.Empty;
32-
}
33-
return readOutputByLines ? string.Empty : process.StandardOutput.ReadToEnd().Trim().Replace(@"\\", @"\");
46+
throw new Exception("Exit Code is not 0");
47+
return readOutputByLines ? string.Empty : retout.Trim().Replace(@"\\", @"\");
3448
}
3549
}
3650
catch (Exception exception)

0 commit comments

Comments
 (0)