Getting Return 0 Value From C# Entry Point Method Using Parent And Child Programs

We know that in C# Apps (both Console and Windows), an Entry Point method refers to the Main() of a program. When the application is started, the main method is the first method that is invoked. Main() does have a return type as “int” and many times, we notice that the return value for this Main() seems 0. So, let’s understand the behind the scene scenario of having this 0 return value in Main().

Background

In general, our assumption is (after Google search) return 0 in Main() refers to the status code of the application, such as “Exited Successfully” or if it is 1, then Exited with some errors. But actually speaking, these 0/1 are user-defined values returning to the called program.

The real benefit of using return 0

A Console App/Windows App ends its execution by default and GC will perform the cleanup on all of the allocated resources with respect to the applications. While returning 0 to the OS by the Main(), what is the purpose being sought by the OS? Is it something related to flag bits? Or some kind of process ends?

The answer is NO. Because OS never minds the return value from Main(), it always considers that the particular application was completed successfully. Okay, then what is the use of returning 0? Yes, here is the role of something like Parent and Child process come to the picture.

This return value can be used when there is a situation when we have Parent and Child programs. Just assuming that a Parent program called a Child Program. Once the Child has completed the execution, the program control in the Parent process will look for the status (just assuming) of what really happened to the Child Process. Was it completed successfully or it was stalled during the program execution? This can be validated by the use of “Exit Code” which is returned by the Main(). We are assuming this exit code only as a flag bit. Let me show this Parent and Child program with a simple example.

Let us consider the Parent program as Windows App and Child as a Console App. Please follow the steps in order to get the “Exit Code” value (return value).

Sample Demo Program

  1. Open Visual Studio (any version) and create a new project for Windows Application.
  2. Once the project gets created, please add another project on the top of the above said Windows Application.
  3. The final project structure looks like below.


  4. In “WindowsAppDemo” project, add a LabelBox, TextBox, and Command button, as shown below.


  5. Add the following code in the button click event in order to call the Console App available in this project solution.
    1. private void btnChildApp_Click(object sender, EventArgs e) {  
    2.     Process _process = new Process();  
    3.     ProcessStartInfo start = new ProcessStartInfo(@ "<<Console APP Path>>\ChildAppDemo.exe");  
    4.     start.Arguments = txtName.Text;  
    5.     start.WindowStyle = ProcessWindowStyle.Normal;  
    6.     start.UseShellExecute = false;  
    7.     _process.StartInfo = start;  
    8.     _process = Process.Start(start);  
    9.     if (_process.HasExited) {  
    10.         MessageBox.Show(string.Format("Child Program Exited with {0} at {1}", _process.ExitCode, _process.ExitTime));  
    11.     } else {  
    12.         MessageBox.Show("Error Occurred!");  
    13.     }  
    14. }  
    In the above code, we have assigned the ChildAppDemo (Console App) to the ProcessStartInfo which is holding the information of the target app. 

  6. Now, go to “ChildAppDemo” console app and receive the passing parameter by Windows App and print it on the console (As shown below).
    1. static int Main(string[] args)  
    2. {  
    3.    Console.WriteLine(args[0]);  
    4.    Console.ReadKey();  
    5.    return 0;  
    6. }  
  7. Currently, our project setup is like this - We have a Parent program (Windows App) which is internally calling the Child program (Console App). We will make the Parent program (Windows App) as a startup project and will debug the code to see the return value. Let me show you here. 

    1. Once you executed the Windows App, just provide some text and click on the Command button. Look at the breakpoint here. The Child program is called while it is still running. Meanwhile, if you look at the breakpoint on the called process class, it's showing the properties, such as “Has Exited”, and the Exit Code values seem to be unavailable. 


    2. Hit Enter on the console app. As we have written Console.ReadKey(), the Child App (Console app) still remains running. To resume back to the Parent program (Windows App), just hit Enter on the Console App. Do not press any key except Enter. Again, look at the breakpoint. This time, we will be getting the Exit code as 0. This Is the actual benefit of having return values on the Main(). 


    3. Final output.

You can return an integer value as anything. The same value can be received from “ExitCode” property.

Conclusion

So, this is what I tried to share the details about having returned 0 in the Main(). This is just an experimental program but can be used in situations like, if we do have cascade console apps in a solution and it has been called one by one, we can validate the cascading called projects by using “Exit Code” property.

Thanks much for reading this article.
 
Happy Coding!


Recommended Free Ebook
Similar Articles