CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2005
    Posts
    35

    How do I exit a program early

    Hi,

    I am writing a C# program that runs every night as a scheduled task. When I get specific errors that are non-recoverable, I want to write a log entry and abort further processing. So far, I've yet to discover how to do this (googling and a coupe of C# books to no avail). My background is Java and there the command is simply "System.exit(0)". It exits the program immediately, which is just what I want to do in C#. What is the C# equivalent?

    Thanks,
    Rob

  2. #2
    Join Date
    May 2011
    Location
    Washington State
    Posts
    220

    Re: How do I exit a program early

    Application.Exit();

  3. #3
    Join Date
    Feb 2005
    Posts
    35

    Re: How do I exit a program early

    The other thing that keeps the Java programmer in me banging his head against the wall is trying to find assembly references. Application is (according the MSDN) is in System.Windows namespace. If I try adding "using System.Windows" and it doesn't like it, to wit: The type or namespace name 'Windows' does not exist in the namespace 'System' (are you missing an assembly reference?) . From within Visual Studio I go looking for the reference assembly under the .NET tab and I find several System.Windows.* assemblies and none are System.Windows.Application or even just System>Windows by itself.

    Any ideas?

    ~ Rob

  4. #4
    Join Date
    May 2011
    Location
    Washington State
    Posts
    220

    Re: How do I exit a program early

    Sorry, I assumed you were doing Windows application... for console application, try

    Code:
    System.Environment.Exit(1);
    In truth, I would recommend throwing exceptions back up to the Main() method and allowing the program to exit gracefully by finishing that method out.

    Example:

    Code:
            static void Main(string[] args)
            {
                try
                {
                    // start your process...
                    DoSomething();
                }
                catch (Exception ex)
                {
                    // any exceptions are caught here....
                    Console.WriteLine(ex.Message);
                }
    
                // program exits gracefully here...
            }
    
            private static void DoSomething()
            {
                try
                {
                    for (int i = 0; i < 10; i++)
                    {
                        Console.WriteLine(String.Format("Doing thing {0}...", i));
                        DoSomethingFurther();
                    }
                }
                catch (Exception ex)
                {
                    // you can test for specific exceptions,
                    // if the one you're looking for is caught, throw it on up!
                    throw ex;
                }
            }
    
            private static void DoSomethingFurther()
            {
                try
                {
                    for (int i = 0; i < 5; i++)
                        Console.WriteLine(String.Format("   Doing further thing {0}...", i));
                }
                catch (Exception ex)
                {
                    // you can test for specific exceptions,
                    // if the one you're looking for is caught, throw it on up!
                    // no matter how far nested you get, you can always toss exceptions back up to the top
                    // and handle them at each step of the way however you want.
                    throw ex;
                }
            }
    Last edited by fcronin; May 19th, 2011 at 04:50 PM.

  5. #5
    Join Date
    Jun 2008
    Posts
    2,477

    Re: How do I exit a program early

    Quote Originally Posted by rtanner View Post
    The other thing that keeps the Java programmer in me banging his head against the wall is trying to find assembly references. Application is (according the MSDN) is in System.Windows namespace. If I try adding "using System.Windows" and it doesn't like it, to wit: The type or namespace name 'Windows' does not exist in the namespace 'System' (are you missing an assembly reference?) . From within Visual Studio I go looking for the reference assembly under the .NET tab and I find several System.Windows.* assemblies and none are System.Windows.Application or even just System>Windows by itself.

    Any ideas?

    ~ Rob
    Sounds like you are missing an assembly reference, i.e., go to your project in the solution expolorer, right click references, add reference, System.Windows.dll. Remember that namespaces can be added to from anywhere, so a single namespace can cover multiple assemblies (DLL's)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured