@@ -8,35 +8,17 @@ public static class ExternalProcess
8
8
{
9
9
public static int ExecuteSync ( string toolPath , string arguments )
10
10
{
11
- var process = StartProcess ( toolPath , arguments ) ;
12
-
13
- if ( process == null )
14
- {
15
- return - 1 ;
16
- }
17
-
18
- WaitForExit ( process ) ;
19
-
20
- return process . ExitCode ;
11
+ return RunProcess ( toolPath , arguments ) ;
21
12
}
22
13
23
- public static async Task < int > ExecuteAsync ( string toolPath , string arguments )
14
+ public static Task < int > ExecuteAsync ( string toolPath , string arguments )
24
15
{
25
- var process = StartProcess ( toolPath , arguments ) ;
26
-
27
- if ( process == null )
28
- {
29
- return - 1 ;
30
- }
31
-
32
- await Task . Run ( ( ) => WaitForExit ( process ) ) ;
33
-
34
- return process . ExitCode ;
16
+ return Task . Run ( ( ) => RunProcess ( toolPath , arguments ) ) ;
35
17
}
36
18
37
- private static Process StartProcess ( string toolPath , string arguments )
19
+ private static int RunProcess ( string toolPath , string arguments )
38
20
{
39
- var process = new Process ( ) ;
21
+ Process process = new Process ( ) ;
40
22
41
23
process . StartInfo . FileName = toolPath ;
42
24
process . StartInfo . Arguments = arguments ;
@@ -46,36 +28,38 @@ private static Process StartProcess(string toolPath, string arguments)
46
28
process . StartInfo . CreateNoWindow = true ;
47
29
process . StartInfo . WindowStyle = ProcessWindowStyle . Hidden ;
48
30
31
+ process . ErrorDataReceived += ( sender , errorLine ) =>
32
+ {
33
+ if ( errorLine . Data != null )
34
+ {
35
+ OutputLine ( errorLine . Data ) ;
36
+ }
37
+ } ;
38
+ process . OutputDataReceived += ( sender , outputLine ) =>
39
+ {
40
+ if ( outputLine . Data != null )
41
+ {
42
+ OutputLine ( outputLine . Data ) ;
43
+ }
44
+ } ;
45
+
49
46
try
50
47
{
51
48
process . Start ( ) ;
49
+ process . BeginErrorReadLine ( ) ;
50
+ process . BeginOutputReadLine ( ) ;
51
+ process . WaitForExit ( ) ;
52
52
}
53
53
catch ( Exception error )
54
54
{
55
55
OutputLine ( error . Message ) ;
56
- return null ;
56
+ return - 1 ;
57
57
}
58
58
59
- return process ;
60
- }
61
-
62
- private static void WaitForExit ( Process process )
63
- {
64
- //Handle output
65
- while ( ! process . StandardOutput . EndOfStream || ! process . StandardError . EndOfStream )
66
- {
67
- if ( ! process . StandardOutput . EndOfStream )
68
- {
69
- OutputLine ( process . StandardOutput . ReadLine ( ) ) ;
70
- }
71
-
72
- if ( ! process . StandardError . EndOfStream )
73
- {
74
- OutputLine ( process . StandardError . ReadLine ( ) ) ;
75
- }
76
- }
59
+ int exitCode = process . ExitCode ;
60
+ process . Close ( ) ;
77
61
78
- process . WaitForExit ( ) ;
62
+ return exitCode ;
79
63
}
80
64
81
65
private static void OutputLine ( string str )
0 commit comments