I've written some code to interact with a console app that regularly hangs (due to buggy COM interop I have no control over). My method includes a call to Process.Kill() after a timeout, but it doesn't seem to kill the process--it still appears in Task Manager. Is there something wrong with this code?
private static string CallBuggyConsoleApp(string path, string ext) {
var startInfo = new ProcessStartInfo {
FileName = ConsoleAppPath,
Arguments = String.Format("\"{0}\" {1}", path, ext),
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true
};
using (var proc = Process.Start(startInfo)) {
//The line above should be replaced with:
//using (var proc = new Process()) {
// proc.StartInfo = startInfo;
var output = new StringBuilder();
var error = new StringBuilder();
proc.OutputDataReceived += (_, args) => output.Append(args.Data);
proc.ErrorDataReceived += (_, args) => error.Append(args.Data);
proc.Start();
proc.BeginOutputReadLine();
proc.BeginErrorReadLine();
if (proc.WaitForExit((int)ConsoleAppTimeout.TotalMilliseconds)) {
proc.WaitForExit();
if (proc.ExitCode != 0) {
throw new Exception(String.Format("Pid {0} exited at {1} with exit code {2} and the following error: {3}",
proc.Id, proc.ExitTime, proc.ExitCode, error.ToString()));
}
return output.ToString();
}
proc.CancelOutputRead();
proc.CancelErrorRead();
proc.Kill();
proc.WaitForExit();
throw new Exception(String.Format("Killed pid {0} at {1}", proc.Id, proc.ExitTime));
}
}
in the inner part of the code where you do the first throw you are not calling the Kill.
in general Exception should not be used in the throws, it's more usual to throw some derived classes of it like ApplicationException or other more specialized ones.
beside this, why are you calling start twice? How would it behave calling start only once? Do you see any difference?
Related
There are two endpoints, I am thinking to add one endpoint to start the process and another is to do process communication(stdin/stdin). Is it possible? Or should I use some other ways to do this like websocket?
I am trying to start a process as below.
Process process = new Process();
ProcessStartInfo procStartInfo = new ProcessStartInfo("/bin/sh");
procStartInfo.RedirectStandardError = true;
procStartInfo.RedirectStandardOutput = true;
procStartInfo.RedirectStandardInput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.Arguments = "-c " + Constants.CMDName + args;
process.StartInfo = procStartInfo;
Console.WriteLine("Start res: " + process.Start());
Process is getting started but when I am trying to do stdin/out like below I am getting an error saying StandardIn not redirected.
Process[] processes = Process.GetProcessesByName(Constants.VSDebugProcessName);
if (processes.Length == 0)
{
throw new Exception("Process is not running");
}
Console.WriteLine(JsonSerializer.Serialize(processes[0].StartInfo));
var process = processes[0];
StreamWriter sw = process.StandardInput;
await sw.WriteLineAsync(JsonSerializer.Serialize(payload));
Should I combine these two endpoints or is there any other workaround for this issue?
You can set EnableRaisingEvents = true in the ProcessStartInfo, and add a handler on the process’s OutputDataReceived message to collect the output. The following snippet illustrates the procedure. It also handles error output (stderr).
var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = fileName,
Arguments = arguments,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
},
EnableRaisingEvents = true,
};
var output = new StringBuilder();
var error = new StringBuilder();
process.OutputDataReceived += (_, args) =>
{
output.AppendLine(args.Data);
};
process.ErrorDataReceived += (_, args) =>
{
error.AppendLine(args.Data);
};
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
ResultsText.Value = output.ToString();
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
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();
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.
I am attempting to get output to show the currently open documents on my machine, but it comes back NULL no matter what.
StringCollection values = new StringCollection();
var proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "openfiles.exe",
Arguments = "/query /FO CSV /v",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};
proc.Start();
while (!proc.StandardOutput.EndOfStream)
{
string line = proc.StandardOutput.ReadLine();
values.Add(line);
}
foreach (string sline in values)
MessageBox.Show(sline);
Edit:
During further review I see that I am getting an exception issue. During my diag run I get the following:
Proc.BasePriority thre an exception of type System.InvalidOperationException
Edit:
Attempted to pull code as:
string val = proc.StandardOutput.ReadToEnd();
MessageBox.Show(val);
Also a NULL value on return, and Proc still had errors even after proc.start();.
You have to read both the standard output and standard error streams. This is because you can't read them both from the same thread.
To achieve this you have to use the eventhandlers that will be called on a separate thread.
Compile the code as anycpu as openfiles comes in a 32-bit and 64-bit variant. It might not find the executable if there is an architecture mismatch.
The lines that are read from the error stream are prepended with ! > so they stand out in the output.
StringCollection values = new StringCollection();
var proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "openfiles.exe",
Arguments = "/query /FO CSV /v",
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = false
}
};
proc.Start();
proc.OutputDataReceived += (s,e) => {
lock (values)
{
values.Add(e.Data);
}
};
proc.ErrorDataReceived += (s,e) => {
lock (values)
{
values.Add("! > " + e.Data);
}
};
proc.BeginErrorReadLine();
proc.BeginOutputReadLine();
proc.WaitForExit();
foreach (string sline in values)
MessageBox.Show(sline);