What is "Process finished with exit code 500" ? - c#

When I run App1 project to started another process(App2.exe), the program quickly exited itself, and then I got this information.
"Process finished with exit code 500"
Originally, I thought this situation should be very common, but I didn't find the information about this exit code on the search engine.
What the hell is going on? I feel very confused.
//App1
using System.Diagnostics;
var app2 = new Process()
{
StartInfo =
{
FileName = "App2.exe",
UseShellExecute = false,
CreateNoWindow = false
}
};
app2.Start();
// App2
while (true)
{
string input = Console.ReadLine();
Console.WriteLine("Input is " + input);
}
Here is the solution, you can reproduce it on your own computer.
https://github.com/Andy-AO/Shared/tree/master/exit_code_500

The above problem occurs when I run it in Rider, but it does not occur when I run it in Visual Studio.

Related

Trying to automatically rebuild and restart .NET console application on linux

I'm making a small webserver .NET console application on my windows machine that will run on a linux server (amazon os), and I'd like to make it automatically update itself when I push new code to github.
Automatically pulling from github already works by waiting for a webhook and then creating a Process which, as I understand it, practically lets me run a command line from the code.
using System.Diagnostics;
public static class GitApi
{
public static void Pull(string path)
{
Process process = new Process()
{
StartInfo = new ProcessStartInfo()
{
FileName = "git",
Arguments = "pull",
UseShellExecute = false,
RedirectStandardOutput = true,
WorkingDirectory = path
}
};
process.Start();
}
}
What I'd then like to do is to automatically rebuild and restart after pulling from git, by using the dotnet run command in a similar way.
using System.Diagnostics;
public static class Restarter
{
public static void Restart(string path)
{
Process process = new Process()
{
StartInfo = new ProcessStartInfo()
{
FileName = "dotnet",
Arguments = "run",
UseShellExecute = false,
WorkingDirectory = path
}
};
process.Start();
Environment.Exit(0);
}
}
While this seems to work fine on windows, when I try on linux it seems to exit the main process and then start the new one in the background somehow. It still writes in the same screen, but I can't kill it with Ctrl + C. Instead I have to find the process by checking the port that it's using and then use the kill command.
Could I fix this somehow or is there another simple way to rebuild and restart a .NET console application?

StandardOutput.ReadToEnd() hangs in one machine but never in another

Below codes have been always working in one environment.
Now we switched to a new environment (new VM), this codes do not work any more, it just hangs there until I manually close the pop-up window from cmd.exe. Obviously, there is some dead lock. But why it only happens in the new environment?? Even now it still works in the old environment.
public static void Main()
{
Process p = new Process()
{
StartInfo = new ProcessStartInfo()
{
FileName = "c:\\windows\\System32\\cmd.exe",
Arguments = "/C START \"exeName\" /D %SystemDrive%\\somefolder start.bat",
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
WorkingDirectory = "c:\\windows\\System32",
CreateNoWindow = true
}
};
p.Start();
string cv_error = null;
Thread et = new Thread(() => { cv_error = p.StandardError.ReadToEnd(); });
et.Start();
string cv_out = null;
Thread ot = new Thread(() => { cv_out = p.StandardOutput.ReadToEnd(); });
ot.Start();
p.WaitForExit();
Console.WriteLine("process finish " + p.HasExited); // This line prints "process finish true" successfully
ot.Join(); // Stuck here until I manually close the popup window. But it works in another machine with this popup window stay open
et.Join();
Console.WriteLine("success" + p.ExitCode);
Console.ReadLine();
}
The start.bat looks like
#echo off
Echo "something"
mklink /D Logs "D:/logs"
Echo "Starting"
cd %~dp0\someFolder
.\ExeToInstallTools.exe arg1 // it will popup a console window showing the progress and logs
IF ERRORLEVEL 1 goto error_handler
Exit /B 0
:error_handler
Echo "Failed"
Exit /B 1
UPDATE:
I have try to use some stackTrace tool to find the difference: when my app (process=4736) start the cmd process, cmd.exe /C turns into cmd.exe /K, it does not happen in the old machine.
StackTraceToolView

C# Program automation - Program hangs/doesn't work

I've been trying to make a program to automate the process of running different processes on my computer. So far I've got the following program running a console version of BleachBit(It's like CCleaner), the process appears in task manager, it hits around 25kb process RAM then CPU usage goes to 0% and just sits there doing nothing for ages and never quits.
Is there something wrong I'm doing in my code that could cause this to happen?
I've tried editing the app.manifest to make sure the program has to be run as admin in case it needed more privileges
Also when running similar code in a bat file to run the program, it's opens its own windows and runs fine, so I'm not sure. Any help in the right direction would be fantastic.
The code I'm running is below.
static void Main(string[] args)
{
string Log = "";
if (File.Exists(Environment.CurrentDirectory + "\\BleachBit\\bleachbit_console.exe"))
{
Log += "File exists";
Log += RunProgramCapturingOutput("\\BleachBit\\bleachbit_console.exe", "--preset --clean");
}
else
Log += "Program not found. Please place at \\BleachBit\\bleachbit_console.exe";
File.WriteAllText("log.txt", Log);
Console.ReadLine();
}
public static string RunProgramCapturingOutput(string filename, string arguments)
{
ProcessStartInfo processInfo = new ProcessStartInfo()
{
FileName = Environment.CurrentDirectory + filename,
Arguments = arguments,
CreateNoWindow = false,
UseShellExecute = false,
WorkingDirectory = Path.GetDirectoryName(Environment.CurrentDirectory + filename),
RedirectStandardError = false,
RedirectStandardOutput = true
};
Process process = Process.Start(processInfo);
process.WaitForExit();
string output = output = process.StandardOutput.ReadToEnd();
Console.WriteLine("Output: " + output);
process.Close();
return output;
}
Switching these lines to this:
string output = output = process.StandardOutput.ReadToEnd();
process.WaitForExit();
allows to avoid deadlocks. The program seems to be a relatively slow running program due to hard-drive I/O, just give it time and you'll see it complete.
I found this deadlock issue from https://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput(v=vs.110).aspx
Where it states in a code block: "// To avoid deadlocks, always read the output stream first and then wait."

Process WaitForExit not blocking

I am using a FilesystemWatcher class to convert audio to a listenable format as they come in. This worked perfectly for a while, until right around the time we upgraded to 4.5 on that server. Now, to get it to work, I need to debug it and set a breakpoint so the method does not exit before the process runs.
It only takes a few milliseconds for sox to convert the audio. Setting a Thread.sleep(10000) doesnt fix the issue. Downgrading the project to .net 2.0 did nothing.
What's really frustrating, is process.WaitForExit() seemingly does not block. While debugging, I step right over it and wait until the file shows up in the folder.
As always, I really appreciate the assistance.
Attached is the relevant code:
void FileCreated(object sender, FileSystemEventArgs e)
{
string newname = String.Format(#"{0}{1}.mp3", _mp3FolderPath, e.Name.Replace(".wav", ""));
string soxArgs = #"-t wav -r 8k -c 1";
ProcessStartInfo p = new ProcessStartInfo
{
FileName = _soxPath,
Arguments = string.Format("{0} {1} {2}", soxArgs, e.FullPath, newname),
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardError = false,
RedirectStandardInput = false,
RedirectStandardOutput = false
};
using (Process process = Process.Start(p))
{
process.WaitForExit();
Thread.Sleep(10000);
//process.Close();
}
}

app crashes with ProcessStartInfo arguments

I am trying to run a process called "prog.exe" with the arguments "blah $00" (sort of a code) but whatever I try fails.
string file = "blah $00";
string result = string.Empty;
ProcessStartInfo P = new ProcessStartInfo(#"""" + "prog.exe" + #"""");
P.Arguments = #"""" + file + #"""";
P.CreateNoWindow = true;
P.UseShellExecute = false;
P.RedirectStandardOutput = true;
Process.Start(P);
using (Process process = Process.Start(P))
{
using (StreamReader str = process.StandardOutput)
result = str.ReadToEnd();
}
MessageBox.Show(result);
When this code is executed, my program just crashes and I am forced to close it using the Task Manager.
I am not sure what's wrong with my code (am I not setting the arguments correctly?), so any help would be appreciated.
Run your process with given argument from console and see what happens. If result is something you expect, just remove double quotes and this should resolve your problem.
I don't think your program crashes. It just waits for "prog.exe" to finish! I bet, that your program continues running as soon as you are done working with prog.exe and close it - and make sure in task manager that it really is gone.

Categories

Resources