HandbrakeCLI wont automate via C# process - c#

I am trying to automate HandbrakeCLI using C# via the System.Diagnostics.Process class. However, as long as my program is trying to run the process, it seems like the process never advances.
Here is my process setup:
Process process = new System.Diagnostics.Process();
ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
startInfo.FileName = parameters.HandbrakeLocation;
startInfo.Arguments = arguments;
process.StartInfo = startInfo;
process.Start();
string output = string.Empty;
while ((output = process.StandardOutput.ReadLine()) != null)
{
Debug.WriteLine(output);
}
process.WaitForExit();
HandbrakeCLI.exe does appear in my processes list. The Debug.WriteLine(output); line continually prints out "Encoding: task 1 of 1, 0.00 %" and the process never completes. If I kill my C# app then HandbrakeCLI instantly shoots up from 7,000k in memory to 145,000k and then does the encoding that I want it to do. Its like my C# app is holding it back.
I have tried to use Read() instead of ReadLine() and I have tried flushing the StandardOutput stream before and after the read with no success. I have a suspicion that since HandbrakeCLI overwrites stdout when it writes the encoding progress, that it won't act like a normal process when automated via C#.

I figured it out. I was redirecting stderr to my C# application, but not consuming it. The process was hanging until stderr was consumed.

Related

Opening and closing background process using Unity and ProcessStartInfo

I have been trying to write a quick script for my unity VR game to begin recording from an Azure Kinect camera at runtime, and close this script/recording on the close of application. To run their demo recording application, you run k4arecorder.exe output.mkv in a command prompt, and then pressing Ctrl-C to close the camera, which allows the program to save the file properly (see Azure Kinect SDK).
I have been able to successfully open the script and begin recording from the camera with the following code, but I have not been able to enter Ctrl-C (or any text at all) on the popup command prompt in order to stop the camera from running. And I can't use Process.StandardInput.Close() as I am running this with useShellExecute=True
The .exe saves the camera recording after Ctrl-C input, so I do need to close the stdin somehow before stopping the program entirely.
ProcessStartInfo startInfo;
Process process;
void Start()
{
startInfo = new ProcessStartInfo();
startInfo.FileName = #"C:\Program Files\Azure Kinect SDK v1.4.1\tools\k4arecorder.exe";
startInfo.Arguments = System.IO.Directory.GetCurrentDirectory() + "\\output_testing.mkv";
process = new Process {StartInfo = startInfo};
process.Start();
}
I've also tried a different approach, redirecting the StandardInput, but then I don't see any text at all on the popup command prompt, and my attempts to close stdin and print anything to the command prompt have also not worked. For example, I never see anything in the command prompt running:
ProcessStartInfo startInfo;
Process process;
void Start()
{
startInfo = new ProcessStartInfo();
startInfo.FileName = #"C:\Program Files\Azure Kinect SDK v1.4.1\tools\k4arecorder.exe";
startInfo.Arguments = System.IO.Directory.GetCurrentDirectory() + "\\output_testing.mkv";
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.RedirectStandardInput = true;
startInfo.CreateNoWindow = false;
process = new Process {StartInfo = startInfo};
process.Start();
process.StandardInput.WriteLine("testing");
string output = process.StandardOutput.ReadToEnd();
UnityEngine.Debug.Log(output);
}
void OnApplicationQuit()
{
// this will just perpetually wait until I close the window cause cntrl-c not going thru
process.StandardInput.Close();
process.WaitForExit();
process.Close();
}
Any tips immensely appreciated! I feel like there must be a way past this with either method, but I'm coming up short. This is Unity 2019.4.1f1 and Azure Kinect SDK v1.4.1 (though I don't think this is a bug).
Hi one thing that worked for me is using:
void OnApplicationQuit() { process.CloseMainWindow(); }

How to open Putty Process without it opens in another window

using this Code
Process process = new Process();
// p.EnableRaisingEvents = true;
process.EnableRaisingEvents = true;
process.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.LoadUserProfile = false;
process.StartInfo.UseShellExecute = false;
process.StartInfo.FileName = #"C:\putty.exe"; ; // Gets or sets the application or document to start.
process.StartInfo.Arguments = "-serial com31 -sercfg 9600,8,n,1,N";//Gets or sets the set of command-line arguments to use when starting the application
Thread.Sleep(1000);
process.Start();//Starts (or reuses) the process resource that is specified by the StartInfo property of this Process component and associates it with the component.
process.BeginOutputReadLine();
process.WaitForExit(); // wait forever with ping
I'm trying to open putty in console application , but any timr I use it it opens in New windows and I cant get to output of the process using process.OutputDataReceived event
I'm using code very similar to this to open Iperf and it works fine
What am I missing here ?
Putty is not a console application and therefore does not provide output on it's stdout and stderr streams.
And since it is a windowed application it does not care whether you start it with the CreateNoWindow flag. Try and start notepad.exe with that flag, you will see it appearing obviously.
There is however a programm by the creators of Putty that provides the functionality you need.
It's called Plink.exe and you can download it on their homepage

Batch process in c#

I am having a Java batch process which I used in C# to run process. I want to have a testcase to check whether the batch process is running or not.
I used the batch process as:
void batch_process(string process)
{
try
{
string strFilePath = Path.Combine(batch_process_path, process);
ProcessStartInfo psi = new ProcessStartInfo(strFilePath);
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
psi.WorkingDirectory = batch_process_path;
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.UseShellExecute = false;
psi.ErrorDialog = true;
}
}
How can this test be done?
I want to write an unit test case to check whether the process will start or not.
when you Start your new process you need to capture the returned Process, that provides access to newly started process, useful for starting, stopping, controlling, and monitoring applications.
Process exe = Process.Start(psi);
....
if exe.HasExited() ....
You could start the process using
Process process = new Process();
string strFilePath = Path.Combine(batch_process_path, process);
process.StartInfo.FileName = strFilePath;
//this line will hold this thread until the process is done.
process.WaitForExit();
then start the process on a different thread and let that thread fire an event after process.WaitForExit(); is done.
You should first start the process using the ProcessStartInfo you've just created like:
Process process = Process.Start(psi);
then you can use process.HasExited to check if the process has exited. Often, you don't need to do this, as process.WaitForExit() blocks the code until process exits.
I'm a bit unsure of the scenario from your question... but 4 techniques you can use are:
if you have started the process using var process = Process.Start(psi); then:
you can periodically check process.HasExited - http://msdn.microsoft.com/en-us/library/system.diagnostics.process.hasexited.aspx
or you can subscribe to Process.Exited - http://msdn.microsoft.com/en-us/library/system.diagnostics.process.exited.aspx
or you can block on Process.WaitForExit - http://msdn.microsoft.com/en-us/library/system.diagnostics.process.waitforexit.aspx
if your process is started in some other way and has some unique name, then you can inspect the enumeration returned by System.Diagnostics.Process.GetProcesses() to determine if your batch process is currently running.
In general... I'd prefer to use Process.Exited, but the other methods all have their place too!

ProcessStartInfo.UseShellExecute = true and waiting for process exit

I want to use shell executable in order to respect user preferences of application to be started, but I also need to know when that particular application is closed.
Process editProcess = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = filename;
startInfo.Verb = "edit";
startInfo.UseShellExecute = true;
editProcess.StartInfo = startInfo;
// start the default editor
editProcess.Start();
editProcess.WaitForExit();
WaitForExit seems to return when the shell process exits and not when the real process exits.
Is there a better way of knowing when started application is exited than manually parsing registry, finding correct application to start and explicitly start that app without shell execute?
Handle the process exited event:
editProcess.Exited += process_Exited;

Is my process waiting for input?

I am using the Process class to run an exe.
The exe is a 3rd party console application that I do not control.
I wish to know whether the process is waiting for input on the command line.
Should it make any difference, I intend to kill the application should it be waiting for input.
There are suitable events for when there is output from the program waiting to be read, but I cannot see anything similar for when the process is waiting patiently for input.
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = "myapp.exe";
info.CreateNoWindow = true;
info.UseShellExecute = false;
info.RedirectStandardError = true;
info.RedirectStandardInput = true;
info.RedirectStandardOutput = true;
process.StartInfo = info;
process.OutputDataReceived += new DataReceivedEventHandler(process_OutputDataReceived);
process.ErrorDataReceived += new DataReceivedEventHandler(process_ErrorDataReceived);
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
How do I detect that my process is waiting for input?
Depending on what the 3rd party process is doing exactly you could try polling its threads' states:
foreach(ProcessThread thread in process.Threads)
if (thread.ThreadState == ThreadState.Wait
&& thread.WaitReason == ThreadWaitReason.UserRequest)
process.Kill();
Failing that... you can try to
process.StandardInput.Close();
after calling Start(), I conjecture that an exception will be raised in the child process if it's trying to read from standard input.
If the console application has some sort of prompt waiting for input, you could periodically parse out the console output text using the Process.StandardOutput property of the process and wait for said prompt. Once the proper string is detected, you know that it's waiting for input. See http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx.
Use process.StandardInput.writrLine("input");
for sending input to consol in c#

Categories

Resources