Windows Console App - how to terminate (abnormally)

  • Thread starter Thread starter PeteZ
  • Start date Start date
P

PeteZ

Hi,

This may seem like a silly question but I'll ask anyway 8^)

I have a C# Windows "Console Application" that when a fatal error occurs,
I'll log the error and I then want to stop the program in its tracks ie.
terminate immeadiately.

Is there a call to do this eg. Application.Terminate ();


- peteZ
 
Hi PeteZ,
Hi,

This may seem like a silly question but I'll ask anyway 8^)

I have a C# Windows "Console Application" that when a fatal error occurs,
I'll log the error and I then want to stop the program in its tracks ie.
terminate immeadiately.

Is there a call to do this eg. Application.Terminate ();


- peteZ

The best practise is to close application with normal
code process e.g.

formMain.Close() // in Windows application
or
return; // from Main() functions in console application.

But there are two other "fast" ways to close application:

1) Application.Exit();

2) create your own exception, e.g. MyExitException()
- throw it in wanted "exit" place
- catch it in Main(...) function e.g.:

public static void Main(string[] args) {
try {
// your application code
}
catch(MyExitException ) {
return;
}
}
 
Marcin Grzêbski said:
Hi PeteZ,
Hi,

This may seem like a silly question but I'll ask anyway 8^)

I have a C# Windows "Console Application" that when a fatal error occurs,
I'll log the error and I then want to stop the program in its tracks ie.
terminate immeadiately.

Is there a call to do this eg. Application.Terminate ();


- peteZ

The best practise is to close application with normal
code process e.g.

formMain.Close() // in Windows application
or
return; // from Main() functions in console application.

But there are two other "fast" ways to close application:

1) Application.Exit();

2) create your own exception, e.g. MyExitException()
- throw it in wanted "exit" place
- catch it in Main(...) function e.g.:

public static void Main(string[] args) {
try {
// your application code
}
catch(MyExitException ) {
return;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top