How to stop ffmpeg process after it has finished processing in C#? - c#

I am trying to stop the ffmpeg process once it has finished doing what I want it do, but I am not able to find a way.
Here is what I have done.
//Process for running ffmpeg
Process process = new Process();
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.FileName = ffmpegfile;
process.StartInfo.Arguments = commandtorun;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.Start();
process.WaitForExit();
process.Close();
The problem is ffmpeg does not tell the process to stop after it has finished executing, so I cant use WaitForExit() call.
What i tried doing is
commandtorun = commandtorun+ " && exit";
to force ffmpeg to close after it finishes executing. Now this works when I try in cmd.
But when I do the same thing in C#, ffmpeg closes down as soon as the command is executed.
Is there any way to force ffmpeg or the process to close after the processing is done ?

Try
process.StartInfo.UseShellExecute = true;
This will force the process to run in shell. I don't know why it did not work using standard execution but sometimes processes behave differently in shell execute.

Related

Can you make it so a C# program executes a CMD command on the same instance? [duplicate]

I figure out how to launch a process. But my problem now is the console window (in this case 7z) pops up frontmost blocking my vision and removing my focus interrupting my sentence or w/e i am doing every few seconds. Its extremely annoying, how do i prevent that from happening. I thought CreateNoWindow solves that but it didnt.
NOTE: sometimes the console needs user input (replace file or not). So hiding it completely may be a problems a well.
This is my current code.
void doSomething(...)
{
myProcess.StartInfo.FileName = ...;
myProcess.StartInfo.Arguments = ...;
myProcess.StartInfo.CreateNoWindow = true;
myProcess.Start();
myProcess.WaitForExit();
}
If I recall correctly, this worked for me
Process process = new Process();
// Stop the process from opening a new window
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
// Setup executable and parameters
process.StartInfo.FileName = #"c:\test.exe"
process.StartInfo.Arguments = "--test";
// Go
process.Start();
I've been using this from within a C# console application to launch another process, and it stops the application from launching it in a separate window, instead keeping everything in the same window.
#galets
In your suggestion, the window is still created, only it begins minimized. This would work better for actually doing what acidzombie24 wanted:
myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
Try this:
myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
I'll have to double check, but I believe you also need to set UseShellExecute = false. This also lets you capture the standard output/error streams.

How do I start a new process with a new window, from a console application?

I'm developing a Console Application in C# that starts and manages other console applications.
When I use System.Diagnostics.Process.Start('anotherapp.exe");, the output from the process is printed to my current console application.
In this instance, I'm not able to redirect the output as some of the applications forcibly control their stdout.
How do I force the new process to start & use it's own window/console?
The following have provided no difference in results:
process.StartInfo.UseShellExecute = true;
process.StartInfo.CreateNoWindow = false;
process.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
The output from the process is printed to its own window only. After starting that process, the managing program is ends/closed. So, the called application is only currently opened.
Try Console.Read() in calling application.
static void Main(string[] args)
{
var process = new Process();
process.StartInfo.FileName = #"D:\repos\check\check\bin\Debug\check.exe"; // just for example, you can use yours.
process.StartInfo.UseShellExecute = true;
process.StartInfo.CreateNoWindow = false;
process.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
process.Start();
Console.Read();
}
I tried this code to call another application. check.exe is opened and displayed its value seperately.
We can also use Thread.Sleep(int.MaxValue)
process.WaitForExit(); can also be used after process.Start(). This will wait for new application to close.

Running program in background from command line

So I have a console application that runs a command line process and then closes. For that second that the process gets called, I can see the window of the process pop up then disappear.
Is it possible to either:
Run the other process in the background
Run the other process minimized so that it still shows on the taskbar but not on the screen.
My code currently:
var proc = new Process();
proc.StartInfo.FileName = "cmd.exe";
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardInput = true;
proc.Start();
proc.StandardInput.WriteLine(#"Navigate to Correct Folder");
proc.StandardInput.WriteLine(#"Run Outside Program");
Add that line :
proc.StartInfo.Arguments = " /c;
And that one :
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
I suspect that you're actually seeing the window of your console application, and not the process you're launching. Change the output type to Windows Application.
Here's a solution that will allow you to show the console still if you need to, for example, if something goes wrong and you want to alert the user.

Determine if process is waiting for user input [duplicate]

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#

System.Diagnostics.Process not Exiting in Code

I have the following code which works well on another server. The problem is that the process never seems to make it to an Exited state. The exe being called creates a file as the last step and this file does get created but my code never seems to know that the process has completed. Also the exe being called runs in much less than 10 seconds when ran manually. My code looks like this:
System.Diagnostics.Process proc = new System.Diagnostics.Process() proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.FileName = exeConf.CMD;
proc.StartInfo.Arguments = argString;
proc.Start();
proc.WaitForExit(10000);
if(proc.HasExited)
msgLine = proc.StandardError.ReadToEnd();
See this MSDN article.
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.
It seems as if Process.StandardOutput.ReadToEnd() has to be called immediately after Process.Start() else it could create a deadlock.

Categories