-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathRunUtils.cs
More file actions
72 lines (61 loc) · 1.77 KB
/
RunUtils.cs
File metadata and controls
72 lines (61 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using System;
using System.Diagnostics;
using System.Text;
namespace GeneXus.Application
{
public static class GxRunner
{
public static void RunAsync(
string commandLine,
string workingDir,
string virtualPath,
string schema,
Action<int> onExit = null)
{
var stdout = new StringBuilder();
var stderr = new StringBuilder();
using var proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = commandLine,
WorkingDirectory = workingDir,
UseShellExecute = false, // required for redirection
CreateNoWindow = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
RedirectStandardInput = false, // flip to true only if you need to write to stdin
StandardOutputEncoding = Encoding.UTF8,
StandardErrorEncoding = Encoding.UTF8
},
EnableRaisingEvents = true
};
proc.StartInfo.ArgumentList.Add(virtualPath);
proc.StartInfo.ArgumentList.Add(schema);
proc.OutputDataReceived += (_, e) =>
{
if (e.Data is null) return;
stdout.AppendLine(e.Data);
Console.WriteLine(e.Data); // forward to parent console (stdout)
};
proc.ErrorDataReceived += (_, e) =>
{
if (e.Data is null) return;
stderr.AppendLine(e.Data);
Console.Error.WriteLine(e.Data); // forward to parent console (stderr)
};
proc.Exited += (sender, e) =>
{
var p = (Process)sender!;
int exitCode = p.ExitCode;
p.Dispose();
Console.WriteLine($"[{DateTime.Now:T}] Process exited with code {exitCode}");
// Optional: call user-provided callback
onExit?.Invoke(exitCode);
};
if (!proc.Start())
throw new InvalidOperationException("Failed to start process");
Console.WriteLine($"[{DateTime.Now:T}] MCP Server Started.");
}
}
}