Process.cs Interoperability

Geçtiğimiz hafta acceptance testlerimizi yazarken in house gelistirilen bir test aracını başlatıp stdout dan çıktı alacak test adapter’ı yazma ihtiyacımız oldu. Windows ortamında sorunsuz kodumuz mono üzerinde koşarken bir hata ile karşılastım.

 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
public string Run(string executablePath, string arguments)
{
    StringBuilder output = new StringBuilder();

    var process = new Process()
    {
        EnableRaisingEvents = true,
        StartInfo = new ProcessStartInfo()
        {
            FileName = executablePath,
            Arguments = $"{arguments}",
            UseShellExecute = false,
            CreateNoWindow = true,
            RedirectStandardInput = true,
            RedirectStandardOutput = true,
            RedirectStandardError = true,
        }
    };

    process.OutputDataReceived += (sender, eventArgs) => output.AppendLine(eventArgs.Data); // <---

    process.Start();
    process.BeginOutputReadLine();
    process.WaitForExit(1000);

    return EscapeTerminalChars(output.ToString());
}

Yukarıdaki kod bloğu windows ortamında beklediğimiz şekilde çalıştı ancak mono clr üzerinde koşturduğumda işaretli satırdaki handler ı çalıştırmadığını gördük. Muhtemel hata ise mono geliştiricilerinin eksik kontrol yapmasından kaynaklanıyor.

Satır satır okuma yapmak zorunda olmadığımız icin aşağıdaki gibi değiştirdik.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
public string Run(string executablePath, string arguments)
{
    var process = new Process()
    {
        EnableRaisingEvents = true,
        StartInfo = new ProcessStartInfo()
        {
            FileName = executablePath,
            Arguments = $"{arguments}",
            UseShellExecute = false,
            CreateNoWindow = true,
            RedirectStandardInput = true,
            RedirectStandardOutput = true,
            RedirectStandardError = true,
        }
    };

    process.Start();
    process.WaitForExit(1000);

    return EscapeTerminalChars(process.StandardOutput?.ReadToEnd());
}

Hem MS hem de Mono CLR üzerinde istediğimiz sonucu elde ettik.