Running program in background from command line - c#

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.

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.

How to stop ffmpeg process after it has finished processing in 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.

Loading bar while cmd.exe is running

What I'm trying to do:
Pass a command to .cmd, show a loading bar while the command executes, exit cmd and display a message box after progress bar is full
What's happening:
When I click the button that sends the command, the application hangs, the command is executed, but CMD never exits after it's finished, so the application remains frozen (until I manually close cmd.exe). I also have no idea how to display a loading bar while the command executes. When the loading bar is full, that's when I'll display the message box.
My code:
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.WorkingDirectory = #"C:\"
p.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.Start();
p.StandardInput.WriteLine(Command_That's_Called);
^ Which gets executed upon a button_Click event.
Things I've tried:
p.WaitForExit(); // still hangs
Also threading, but I got an error like "accessed from thread other than one it was created on".
Regarding CMD not closing, I'd just kill it after a certain amount of time, but it the length of time for the command to complete depends on various things.
To exit the CMD process after it finished to execute the command, try to add "/C" at the beginning of your process arguments:
p.StartInfo.Arguments = "/C (your arguments)";
If you type "cmd /?" in the command prompt, you'll find that "/C: Carries out the command specified by string and then terminates".
About to add a LoadingBar, you need to learn about BackgroundWorker class

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