Skip to content

Commit f98af98

Browse files
committed
Fix potential output blocks when triggering external process
1 parent cdd447d commit f98af98

File tree

2 files changed

+28
-44
lines changed

2 files changed

+28
-44
lines changed

CompileScore/Shared/Editor/ExternalProcess.cs

Lines changed: 27 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,17 @@ public static class ExternalProcess
88
{
99
public static int ExecuteSync(string toolPath, string arguments)
1010
{
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);
2112
}
2213

23-
public static async Task<int> ExecuteAsync(string toolPath, string arguments)
14+
public static Task<int> ExecuteAsync(string toolPath, string arguments)
2415
{
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));
3517
}
3618

37-
private static Process StartProcess(string toolPath, string arguments)
19+
private static int RunProcess(string toolPath, string arguments)
3820
{
39-
var process = new Process();
21+
Process process = new Process();
4022

4123
process.StartInfo.FileName = toolPath;
4224
process.StartInfo.Arguments = arguments;
@@ -46,36 +28,38 @@ private static Process StartProcess(string toolPath, string arguments)
4628
process.StartInfo.CreateNoWindow = true;
4729
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
4830

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+
4946
try
5047
{
5148
process.Start();
49+
process.BeginErrorReadLine();
50+
process.BeginOutputReadLine();
51+
process.WaitForExit();
5252
}
5353
catch (Exception error)
5454
{
5555
OutputLine(error.Message);
56-
return null;
56+
return -1;
5757
}
5858

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();
7761

78-
process.WaitForExit();
62+
return exitCode;
7963
}
8064

8165
private static void OutputLine(string str)

CompileScore/VSIX17/source.extension.vsixmanifest

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<PackageManifest Version="2.0.0" xmlns="http://schemas.microsoft.com/developer/vsx-schema/2011" xmlns:d="http://schemas.microsoft.com/developer/vsx-schema-design/2011">
33
<Metadata>
4-
<Identity Id="CompileScore.6e13b50f-06b4-40b3-8168-97c8ac3974d0" Version="1.8.3.1" Language="en-US" Publisher="Ramon Viladomat" />
4+
<Identity Id="CompileScore.6e13b50f-06b4-40b3-8168-97c8ac3974d0" Version="1.8.4" Language="en-US" Publisher="Ramon Viladomat" />
55
<DisplayName>Compile Score 2022</DisplayName>
66
<Description xml:space="preserve">C/C++ compile times data viewer and text highlight for Clang and MSVC.</Description>
77
<MoreInfo>https://github.com/Viladoman/CompileScore</MoreInfo>

0 commit comments

Comments
 (0)