Git for windows seems to create sub-processes that never die - c#

I use the following code to call git from C#:
var pInfo = new ProcessStartInfo
{
FileName = "git",
Arguments = "checkout master",
UseShellExecute = false
};
using var p = Process.Start(pInfo);
Console.WriteLine(p.Id);
p.WaitForExit();
The process ID printed is, lets say 3709. When my program ends, I look at the Task Manager and I see a git.exe process still running with a different ID, say 8865. This process remains running indefinitely.
It seems to me that the git process started by my program spawns a second git process that never exits. Is there any way for me to prevent this behavior? As it is, every time I run my program, there is yet another git.exe process left behind indefinitely, adding up to a lot over time, until I reboot.
(Alternatively, is there any other way to invoke git other than by running the executable?)

Related

Executing cmd commands with complex output in c#

I often find myself using different commands on pc's to check stuff in the system and I wanted to basically create a small standalone exe that will execute all my common commands and give me the output of each of them without me entering them manually...
I want to note that I know this isn't the first question on google on the subject of executing a cmd command with c# but non fit my command requirements, for example many of them execute commands such as copy or move or make and non of them have a complex output, I want to execute for example the "sfc /scannow" command, which outputs a progress bar which measures the progress of course and a final output, the issue with that is I have tried many ways to attempt that but all failed, a shell execution works very well with opening another cmd windows and even requiring elevation but as soon as the progress bar ends and it displays the final output it crashes (and thus I can't see the final output), with shell execution off (executing via the main window) it either doesn't show the progress bar at all and just shows the final output, or shows nothing, or it does show the progress bar, but with each increment it's a new line which obviously doesn't look right...
this is my code:
public static void ExecuteCommand(string command) {
Process prc = new Process();
ProcessStartInfo info = new ProcessStartInfo
{
Verb = "runas",
FileName = "cmd.exe",
UseShellExecute = true,
Arguments = "/c" + command
};
prc.StartInfo = info;
prc.Start();
prc.WaitForExit();
prc.Close();
}
if anyone can think of a fix please do tell me, I have been stuck on this for about 3 days...
Turns out the culprit was "/c", if I change it to "/K" the windows stays open until I close it manually like I wanted.

How to pass values from a process being called

In the main method I want to start a process which is to run some test cases, and when the process returns I want to know how many test cases failed. In this case, I want the process to be able to return a value to the main process after it is done executing. Is there a way to do this? My code is like below:
ProcessStartInfo processInfo = new ProcessStartInfo(exeFilePath, Parser.Default.FormatCommandLine(options));
var p = Process.Start(processInfo);
// need to get the number of failed test cases from the process
Any help is appreciated.

launch exe with params, but program closes instantly after opening?

I am writing a client for my gaming community and one of the functions of this client is to launch a game via the client with parameters that will enable our community mod pack on launch.
When I press the button, the game begins to launch and as soon as the program opens (the icon pops up in the task bar), it closes instantly.
Is there something I am missing that is needed to keep the launched exe running?
Here is my code:
private void btnLaunchGame_Click(object sender, EventArgs e)
{
string armaPath = gameDir+"/Expansion/beta/";
string filename = Path.Combine(armaPath, "arma2oa.exe");
string launchParams = "-noSplash -noFilePatching -showScriptErrors \"-name=Meta\" \"-mod=I:/Steam/steamapps/common/Arma 2;expansion;expansion/beta;expansion/beta/expansion;servermods/#HC_DAYZ;servermods/#HC_WEAPONS;servermods/#HC_EXTRAS;servermods/#HC_ACE\"";
System.Diagnostics.Process.Start(filename, launchParams);
}//close Game Launch
Any ideas is appreciated!
I have a .bat file that will execute the game flawlessly with the launch args listed below, this could possibly help pinpoint the cause of my problem:
http://puu.sh/5CGKk.png (couldn't get code to paste in a readable format).
Try using Process:
Process process = new Process();
process.StartInfo.FileName = "arma2oa.exe";
process.StartInfo.Arguments = "-noSplash -noFilePatching -showScriptErrors \"-name=Meta\" \"-mod=I:/Steam/steamapps/common/Arma 2;expansion;expansion/beta;expansion/beta/expansion;servermods/#HC_DAYZ;servermods/#HC_WEAPONS;servermods/#HC_EXTRAS;servermods/#HC_ACE\"";
process.StartInfo.WorkingDirectory = gameDir + "/Expansion/beta/";
process.Start();
It may be what exe require working directory to be set. Or it will crash, unable to load resources.
If that doesn't works, then perhaps you need to add
process.WaitForInputIdle();
before exiting function running process. I don't know why, but running Acrobat Reader without this wait may sometimes cause a wierd effect: Acrobat is running, but the document, passed via arguments, is not shown. Perhaps something to do with Garbage collector or Process itself.
Try following
using (Process process = new Process())
{
ProcessStartInfo startInfo = new ProcessStartInfo("C:\Program Files\Arma2oa\Arma2oa.exe");
startInfo.Arguments = "-noSplash -noFilePatching -showScriptErrors \"-name=Meta\" \"-mod=I:/Steam/steamapps/common/Arma 2;expansion;expansion/beta;expansion/beta/expansion;servermods/#HC_DAYZ;servermods/#HC_WEAPONS;servermods/#HC_EXTRAS;servermods/#HC_ACE\"";
process.StartInfo = startInfo;
process.Start();
}

Different reference returned from Process.Start

ProcessStartInfo psi = new ProcessStartInfo(BatchFile)
Process p = Process.Start(psi)
Why p.ID is different than process id visible in WindowsTaskManager
(BatchFile is path to external program with appropriate parameters)
I would assume that it's because p.ID is the id of the process that's running the batch file rather than the id of the process started by the batch file.
You can start the executable directly by Process.Start by using the correct overload
I assume BatchFile is some kind of cmd or bat file that runs other processes one by one.
So in Windows Task Manager you actually see ids of those processes that are run by batch file.
Examples
If I do this
var p = Process.Start("notepad.exe");
p.Id will match to the PID from Task Manager.
However, if I do this:
var p = Process.Start("test.cmd"); // test.cmd has notepad.exe call inside
p.Id will be different from PID shown in the Task Manager.
A process ID is only meaningful while the process is alive. The first thing to check is .HasExited - if this is true, ignore the process ID; it no longer has any meaning.
There are a number of ways you can start something and have no process left even though you can apparently see it still on screen:
if it is a script/bat/cmd that spawns something and exits (remember: you are watching the script, not the "something")
if the exe does some inter-exe voodoo internally - for example, most of the office apps and internet explorer do this; if there is an existing process, it forwards the args to that process to handle, and exits immediately

How to continue executing other processes in command line window after breaking a running process using C#

I have written C# command window application. I'm running bunch of processes on command line inside the main(). For e.g.
void main()
{
process p1 = new process()
set p1 properties
p1.start()
-->p1.StandardInput.WriteLine("start /WAIT cmd.exe /c BUILD -cZP");
}
This line will execute some program in a new command window. While executing that last line I will break this execution using ctrl+c and return the control to execution of the main program.
loop through to output to the execution window.
p1.StandardInput.WriteLine("Done some action");
p1.WaitForExit();
p1.Close();
The above three lines are not executed. The question is p1 never closes to execute following lines which I have in my program.
process p2 = new process()
...
p2.waitforExit()
p2.close.
Any insight for above challenges will be great. thx.
If I understand you correctly (which I admit I may not be understanding you), I believe the problem is that when you press CTRL-C to break into process p1, you are actually killing that process. Then you are trying to send text to the standard input for the process, which has just been killed. Since the process is no longer available to take your input, the main program hangs. That's my best guess.

Categories

Resources