Scenario: I have a MFC code which call an exe created in C# (it is a windows form application)
Need: I need that the exe would return a value when closed and on the basis of the return value the same exe will started again
psudocode
int result = RunExe("exename", arguments)
if(result == 1)
{
result = RunExe("exename", arguments)
}
do I have to put the if condition in loop?
plz give me some suggestion. 1.How to return a value from exe 2. How to collect the return value 3. How to restart the exe
Your C# EXE can return an int value like this:
[STAThread]
public static int Main() {
return 5;
}
Your other app has to handle the return value like the others here has explained.
var proc = Process.Start("mycsharwinformapp.exe"):
proc.WaitForExit();
//If the code is 5 restart app!
if(proc.ExitCode==5) Process.Start("mycsharwinformapp.exe"):
The following method should do the trick;
private static int RunProcess(string processName, string arguments)
{
Process process = new Process();
process.StartInfo.FileName = processName;
process.StartInfo.Arguments = arguments;
process.Start();
process.WaitForExit();
return process.ExitCode;
}
Then call it like so;
int returnCode;
do
{
returnCode = RunProcess("...", "...");
}
while (returnCode == 1);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With