public async Task RunAsync(Graph graph, Stream outputStream, RendererLayouts layout, RendererFormats format, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
var fileName = Path.Combine(graphvizBin, GetRendererLayoutExecutable(layout));
var arguments = GetRendererFormatArgument(format);
var startInfo = new ProcessStartInfo
{
FileName = fileName,
Arguments = arguments,
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true
};
var tcs = new TaskCompletionSource<object>();
using (var process = Process.Start(startInfo))
{
cancellationToken.ThrowIfCancellationRequested();
cancellationToken.Register(() =>
{
tcs.TrySetCanceled();
process.CloseMainWindow();
});
using (var standardInput = process.StandardInput)
{
graph.WriteTo(standardInput);
}
using (var standardOutput = process.StandardOutput)
{
await Task.WhenAny(tcs.Task, standardOutput.BaseStream.CopyToAsync(outputStream, 4096, cancellationToken));
}
}
cancellationToken.ThrowIfCancellationRequested();
}