Код: Выделить всё
public static int Main(string[] args)
{
var executable = @"hardcoded path to childprocess.dll";
var psi = new ProcessStartInfo(executable)
{
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
};
using var process = Process.Start(psi)!;
process.OutputDataReceived += (_, dataReceivedEventArgs) => ProcessOnOutputDataReceived(dataReceivedEventArgs, Console.Out);
process.BeginOutputReadLine();
process.WaitForExit();
return process.ExitCode;
}
private static void ProcessOnOutputDataReceived(DataReceivedEventArgs args, TextWriter textWriter)
{
// never called for Console.Write + Flush
// called without issues with Console.WriteLine
if (args.Data is var data and not null)
{
textWriter.WriteLine(data);
}
}
Это прекрасно работает, если дочерний процесс использует Console.WriteLine, но я не могу заставить его работать с Console.Write + Flush, что, как я полагаю, будет эквивалентно.
п>
Код: Выделить всё
public static void Main(string[] args)
{
Console.Write("Output never visible :(");
Console.Out.Flush();
// Console.WriteLine("This works perfectly fine!");
Console.ReadLine(); // just to stop the process from quitting.
}
Для сброса мы попадаем в ConsolePal ::WriteFileNative с параметром useFileAPI, установленным в значение true, что означает, что в конечном итоге мы вызываем Kernel32.WriteFile, который возвращает правильно записанное количество символов.
Вызов FlushFileBuffers с дескриптором файла также не имеет значения.
Подробнее здесь: https://stackoverflow.com/questions/793 ... -writeline