Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get return value from exe and restart it

Tags:

c#

mfc

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

like image 374
sshah Avatar asked Dec 22 '22 15:12

sshah


2 Answers

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"): 
like image 76
Wolf5 Avatar answered Jan 05 '23 18:01

Wolf5


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);
like image 26
Chris McAtackney Avatar answered Jan 05 '23 19:01

Chris McAtackney