C# headless chrome from Process Start not working - c#

I need to call headless chrome from a net core console application. But with this code the aplication run and get stuck doing nothing and printing nothin, also the pdf is not created. The same arguments in the terminal are working as expected.
public static bool TakeScreenshot2()
{
try
{
var procStartInfo = new ProcessStartInfo()
{
FileName = "google-chrome",
Arguments = "--headless --disable-gpu --print-to-pdf=final.pdf http://www.google.com/",
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true
};
var proc = new Process { StartInfo = procStartInfo };
proc.Start();
var output = proc.StandardOutput.ReadToEnd();
Console.WriteLine(output);
string error = proc.StandardError.ReadToEnd();
Console.WriteLine(error);
return proc.ExitCode == decimal.Zero ? true : false;
}
finally
{
// do something
}
}

You should wait for the process to finish
var proc = new Process { StartInfo = procStartInfo };
proc.Start();
proc.WaitForExit();
You can check if it was success also with proc.ExitCode after it exit
If you dont want to block the thread unit it finish you can run it with, you function needs to be async
await Task.Run(() => proc.WaitForExit());
or to use the Process event Exited

Related

Not possible to set input file in linux command running from .NET Core

I'm running .NET Core app on the linux docker container
When I call the command from the linux terminal it works well:
./darknet detector test -out result.json < data/file-list.txt
But when I start the process from the .NET Core I see error. Process runner method:
public static string RunCommand(string command, string args)
{
var process = new Process()
{
StartInfo = new ProcessStartInfo
{
FileName = command,
Arguments = args,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true
}
};
process.Start();
process.WaitForExit();
string output = process.StandardOutput.ReadToEnd();
string error = process.StandardError.ReadToEnd();
return #$"{output}{Environment.NewLine}-------------------------------{Environment.NewLine}{error}";
}
Calling code:
string args = #$"detector test -out result.json < data/file-list.txt";
string output = ProcessRunner.RunCommand("./darknet", args);
Here is the part of the output:
Cannot load image "<"
STB Reason: can't fopen
How to fix it?
You can write the process's standard input once you set the RedirectStandartInput to true while starting your process. Here is an example how to write :
var process = new Process()
{
StartInfo = new ProcessStartInfo
{
FileName = "./ConsoleApp1.exe",
RedirectStandardOutput = true,
RedirectStandardError = true,
RedirectStandardInput = true, // here you need
UseShellExecute = false,
CreateNoWindow = true,
}
};
process.Start();
using var file = File.OpenRead("./1.txt");
using var reader = new StreamReader(file);
while (true)
{
var line = await reader.ReadLineAsync();
if (string.IsNullOrWhiteSpace(line)) break; // you can use some other stoping decision
await process.StandardInput.WriteLineAsync(line);
}

Getting the error of a Powershell Script within c# and outputting it with conditional statement?

I have a console application and a method that executes a PowerShell script within the console application. So I'm trying to grab an error text that it outputs in the application and do something with it.
Example/What I'm trying to do:
If Error.contains("Object")
{
// do something here
}
Here is my current method
public void ExecutePowershellScript()
{
var file = #"C:\Path\filename.ps1";
var start = new ProcessStartInfo()
{
FileName = "powershell.exe",
Arguments = $"-NoProfile -ExecutionPolicy unrestricted -file \"{file}\"",
UseShellExecute = false
};
Process.Start(start);
}
Process.start: how to get the output?
When you create your Process object set StartInfo appropriately:
var proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "program.exe",
Arguments = "command line arguments to your executable",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};
then start the process and read from it:
proc.Start();
while (!proc.StandardOutput.EndOfStream)
{
string line = proc.StandardOutput.ReadLine();
// do something with line
}
You can use int.Parse() or int.TryParse() to convert the strings to numeric values. You may have to do some string manipulation first if there are invalid numeric characters in the strings you read.
You can set RedirectStandardError = true and access any errors from process.StandardError
public static void ExecutePowershellScript()
{
var file = #"C:\Path\filename.ps1";
var start = new ProcessStartInfo()
{
FileName = "powershell.exe",
Arguments = $"-NoProfile -ExecutionPolicy unrestricted -file \"{file}\"",
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true
};
using Process process = Process.Start(start);
string output = process.StandardOutput.ReadToEnd();
string errors = process.StandardError.ReadToEnd();
}
Okay, scratch the above suggestion.
After being corrected by mklement0,
This is a perfectly reasonable attempt, but, unfortunately, it can lead to hangs (while waiting for one's stream end, the other, when exceeding the buffer size, may cause process execution to block). If you need to capture both streams, you must collect the output from one of them via events. – mklement0
I changed the solution to use the ErrorDataReceived event
public static async Task ExecutePowershellScript()
{
var file = #"C:\Path\filename.ps1";
var start = new ProcessStartInfo
{
FileName = "powershell.exe",
Arguments = $"-NoProfile -ExecutionPolicy unrestricted -file \"{file}\"",
UseShellExecute = false,
// redirect standard error stream to process.StandardError
RedirectStandardError = true
};
using var process = new Process
{
StartInfo = start
};
// Subscribe to ErrorDataReceived event
process.ErrorDataReceived += (sender, e) =>
{
// code to process the error lines in e.Data
};
process.Start();
// Necessary to start redirecting errors to StandardError
process.BeginErrorReadLine();
// Wait for process to exit
await process.WaitForExitAsync();
}
start.Start();
while (!start.StandardOutput.EndOfStream)
{
string line = start.StandardOutput.ReadLine();
}

How to execute a shell command across platforms in .NET Core?

In my .NET Core Console app, I receive multiple commands in form of an array of string, and would like to execute them as console command (and showing their output in my own app if possible but not hard requirement).
At first, I tried to parse each command to separate their name and arguments and put them in ProcessStartInfo. However, some command does not work (even simple commands like echo "Hello").
Now I switched to call Powershell instead like this:
static IEnumerable<ProcessStartInfo> ParseCommands(string[] args)
{
return args
.Skip(1)
.Select(q => new ProcessStartInfo()
{
FileName = "powershell",
Arguments = q,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
}).ToList();
}
static void RunCommand(ProcessStartInfo processInfo)
{
Console.WriteLine($"{processInfo.Arguments}");
var process = new Process()
{
StartInfo = processInfo,
};
process.Start();
while (!process.StandardOutput.EndOfStream)
{
Console.WriteLine(process.StandardOutput.ReadLine());
}
process.WaitForExit();
}
The problem is I don't think this one can run on Linux or MacOS. Is there any "standard" way to tell my app to "run this as if it's a console command"?
This is my current code by using the Platform to determine the console command, feel free to tell me if there is a better way:
static IEnumerable<ProcessStartInfo> ParseCommands(string[] args)
{
var argsPrepend = "";
var shellName = "/bin/bash";
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
shellName = "cmd";
argsPrepend = "/c ";
}
return args
.Skip(1)
.Select(q => new ProcessStartInfo()
{
FileName = shellName,
Arguments = argsPrepend + q,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
}).ToList();
}
static void RunCommand(ProcessStartInfo processInfo)
{
Console.WriteLine($"{processInfo.Arguments.Substring(processInfo.FileName == "cmd" ? 3 : 0)}");
var process = new Process()
{
StartInfo = processInfo,
};
process.Start();
while (!process.StandardOutput.EndOfStream)
{
Console.WriteLine(process.StandardOutput.ReadLine());
}
process.WaitForExit();
}

Process execution takes too long when it is called from WPF

I have the following code which opens command window (from WPF interface) and executes code where can take long like # 8-10 minutes:
ProcessStartInfo procStartInfo = new ProcessStartInfo();
procStartInfo.FileName = _exePath;
procStartInfo.Arguments = arguments;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = false;
procStartInfo.RedirectStandardOutput = true;
procStartInfo.RedirectStandardError = true;
using (Process pr = Process.Start(procStartInfo))
{
pr.WaitForExit();
string result = pr.StandardOutput.ReadToEnd();
string[] split = result.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
int output = 0;
int.TryParse(split[split.Length - 1], out output);
return output;
}
And in Program.cs I have method which update status (show operation status and percent) with:
Console.Title = "Loading ... 5 %";
// do request to server and check status
while(status.InProgress) {
Thread.Sleep(2000); // 2 seconds
status = web.getJsonFromApiServer(url); // {InProgress: "true", Message: "Checking X%";
}
Sometimes the process is hanged and its title is not updated anymore like something goes in infinite loop.
If I use console without starting from WPF ( I mean use command prompt and then set location to exe path and run it with arguments), it works fine, no issue.
Why does this thing happens ?
A deadlock condition can result if the parent process calls
p.WaitForExit before p.StandardOutput.ReadToEnd and the child process
writes enough text to fill the redirected stream. The parent process
would wait indefinitely for the child process to exit. The child
process would wait indefinitely for the parent to read from the full
StandardOutput stream.
To avoid deadlocks you should use asynchronous methods:
var procStartInfo = new ProcessStartInfo
{
FileName = _exePath,
Arguments = arguments,
UseShellExecute = false,
CreateNoWindow = false,
RedirectStandardOutput = true,
RedirectStandardError = true
};
var p = new Process { StartInfo = procStartInfo };
p.OutputDataReceived += (sender, eventArgs) => { Console.WriteLine(eventArgs.Data); };
p.Start();
p.BeginOutputReadLine();
p.WaitForExit();

Redirect input and output for cmd.exe

I wanna redirect cmd.exe output somewhere, below code works when the command is a line:
Process p = new Process()
{
StartInfo = new ProcessStartInfo("cmd")
{
UseShellExecute = false,
RedirectStandardInput = true,
RedirectStandardOutput = true,
CreateNoWindow = true,
Arguments = String.Format("/c \"{0}\"", command),
}
};
p.OutputDataReceived += (s, e) => Messagebox.Show(e.Data);
p.Start();
p.BeginOutputReadLine();
p.WaitForExit();
But how about a series commands like WriteLine():
p.StandardInput.WriteLine("cd...");
p.StandardInput.WriteLine("dir");
how to get output in this situation?
To achieve such behavior you should use /k switch to run cmd.exe in interactive mode.
The problem is to separate inputs from different commands.
To do this you could change the standard prompt using prompt command:
prompt --Prompt_C2BCE8F8E2C24403A71CA4B7F7521F5B_F659E9F3F8574A72BE92206596C423D5
So now it is pretty easy to determine the end of command output.
Here is the complete code:
public static IEnumerable<string> RunCommands(params string[] commands) {
var process = new Process {
StartInfo = new ProcessStartInfo("cmd") {
UseShellExecute = false,
RedirectStandardInput = true,
RedirectStandardOutput = true,
CreateNoWindow = true,
Arguments = "/k",
}
};
process.Start();
const string prompt = "--Prompt_C2BCE8F8E2C24403A71CA4B7F7521F5B_F659E9F3F8574A72BE92206596C423D5 ";
// replacing standard prompt in order to determine end of command output
process.StandardInput.WriteLine("prompt " + prompt);
process.StandardInput.Flush();
process.StandardOutput.ReadLine();
process.StandardOutput.ReadLine();
var result = new List<string>();
try {
var commandResult = new StringBuilder();
foreach (var command in commands) {
process.StandardInput.WriteLine(command);
process.StandardInput.WriteLine();
process.StandardInput.Flush();
process.StandardOutput.ReadLine();
while (true) {
var line = process.StandardOutput.ReadLine();
if (line == prompt) // end of command output
break;
commandResult.AppendLine(line);
}
result.Add(commandResult.ToString());
commandResult.Clear();
}
} finally {
process.Kill();
}
return result;
}
It works well but it looks like one big hack.
I'd recommend you to use process per command instead.

Categories

Resources