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
Re: How do I exit a program early
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
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;
}
}
Re: How do I exit a program early
Quote:
Originally Posted by
rtanner
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)