DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
11 Monitoring and Observability Tools for 2023
Learn more

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

In this post, we check out one developer's experimental code, and why he believes it can help fellow devs. Read on to learn more!

Prabu E user avatar by
Prabu E
·
Sep. 18, 18 · Tutorial
Like (6)
Save
Tweet
Share
13.19K Views

Join the DZone community and get the full member experience.

Join For Free

Hello everyone, as we know that in C# apps (both Console and Win) 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. This Main() does have a return type of “int” and much of the time we noticed that the return value for this Main() seems to 0. So let’s understand the behind the scenes of this return value.

Background

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

The Real Benefit of Using Return 0

If a console application/windows application ends its execution by default, GC will perform clean up all of the allocated resources with respect to the applications. What is the purpose of returning 0 to the OS by the Main() method? Is it something related with flag bits? Or something where a kind of process ends? The answer is no. Because the OS never minds the return value from Main(), it always considers the particular application to have been completed successfully. Okay, then what is the use of returning 0? Yes, here is where the role of something like Parent and Child Processes come into place.

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 then 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 program execution? This can be validated by the use of “Exit Code,” which is returned by Main(). This only exits code (we are assuming) as a flag bit. Let me show this Parent and Child Program with a simple example.

Considering 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 a Windows Application.
  2. Once the project is created, please add another project on top of the above Windows Application.
  3. The final Project structure looks like below:

Image title

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

Image title

5. Add the following code to the button click event, in order to call the Console App which is available in this project solution.

Image title

In the above code, first, we have assigned ChildAppDemo (Console App) to ProcessStartInfo, which is holding the information about the target app.

6. Now, go to the “ChildAppDemo” console app, and receive the passing parameter by Windows App and print it on the console (as shown below).

Image title

7. Currently, our project setup so: We have a Parent program (Windows App) which is internally calling the Child Program (Console App). Make the Parent program (Windows App) a startup project and debug the code to see the return value. Let me show you here.

  • 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 and is still running; meanwhile, if you look at the breakpoint on the called process class, it's showing properties such as “Has Exited” and the Exit Code values seems to be unavailable.

Image title

  • Hit enter on the console app. As we wrote Console.ReadKey() so the Child App (Console app) remains running, to go 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 it will be getting the exit code as a 0. This is the actual benefit of having return values to Main().

Image title

  • Final output:

Image title

Conclusion

I've tried to share the details about having return 0 in the Main() method. This is just an experimental program. It can be used assuming we have cascade console apps in a solution and it has been called, one-by-one, meaning we can validate the called cascading projects by using the “Exit Code” property.

Thanks much for reading this article.

Happy coding!

app

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How To Create a Failover Client Using the Hazelcast Viridian Serverless
  • What Is Continuous Testing?
  • 5 Ways to Secure a Virtual Machine in Cloud Computing
  • What’s New in the Latest Version of Angular V15?

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: