[ACCEPTED]-How do I return a value from a console application to a service in .NET?-console-application
Process p = Process.Start("app.exe");
p.WaitForExit();
int number = p.ExitCode;
And in app.exe you set Enviroment.ExitCode...
0
you can make use of an Exit Code by changing 2 your Main method from
public void Main(string[] args){return;}
to
public int Main(string[] args){return 0;}
Not an elegant solution, but 1 it works.
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
.
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.
The console app can set its exit code.
The 3 console app can set its exit code by either:
- Call
System.Environment.Exit(exitcode)
, or - 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
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.