[ACCEPTED]-How do I return a value from a console application to a service in .NET?-console-application

Accepted answer
Score: 12
Process p = Process.Start("app.exe");
int number = p.ExitCode;

And in app.exe you set Enviroment.ExitCode...


Score: 3

you can make use of an Exit Code by changing 2 your Main method from

public void Main(string[] args){return;}


public int Main(string[] args){return 0;}

Not an elegant solution, but 1 it works.

Score: 1

An arbitrary number can be returned via 3 standard ExitCode mechanism. You'd just have to 2 Start() a process, then WaitForExit(), and then get the value 1 of ExitCode.

Score: 1

You can use the exit code, which can be 11 returned from the Main function or set via 10 Environment.ExitCode.

Another option would 9 be to have your ConsoleApp write to the 8 standard output stream, and redirect the 7 output to your main application.

The advantage 6 of the second option is that you could return 5 more data than a single integer, if later 4 you find this necessary. The disadvantage 3 is that it's a bit more work to setup.

This 2 CodeProject article walks through redirecting the standard 1 output streams of a spawned process.

Score: 0

The console app can set its exit code.

The 3 console app can set its exit code by either:

  1. Call System.Environment.Exit(exitcode), or
  2. Let the main function return an int.

The 2 ExitCode property (of the System.Diagnostics.Process class) lets the service 1 process examine the console app's exit code.

More Related questions