Application.Exit

  • Thread starter Thread starter Sanjiv
  • Start date Start date
S

Sanjiv

I have a windows application with a main routine in a class.

I do all the processing in main() routine, displaying modal and
modeless forms, database stuff etc.Need to stop processing as soon as
something bad happens.....

How do you return out of Main() routine? Application.Exit() doesn't
work because there is no Application.Run(form).

Should I use Environment.Exit() ?

Suggestions....

Thanks

Sanjiv
 
You can use Environment.Exit. This is perhaps the most "graceful" way of
exiting an app without actually returning from your main thread, but there
is a cost and you should be aware of some of the limitations of using this
API.

All user threads in the app are suspended and do not resume. This means that
code in finally blocks will not run, so if you have transaction oriented
cleanup in finally blocks this may not actually get executed. Appdomains do
not get unloaded.

Finalizers will still run, so resource-centric cleanup will still get done
(e.g. you can release unmanaged resources that are wrapped by a managed
object in the managed object's finalizer).

This shutdown is graceful in the sense that it is controlled by the clr, and
it is not a hard shutdown like killing the app, but if you need cleanup code
in finally blocks to run then this may not be suitable for you. You could
instead set a global shutdown flag/event that your main routine monitored
and used as a signal to exit.

Dave
 
Thanks Dave

That helps.

Sanjiv



Dave said:
You can use Environment.Exit. This is perhaps the most "graceful" way of
exiting an app without actually returning from your main thread, but there
is a cost and you should be aware of some of the limitations of using this
API.

All user threads in the app are suspended and do not resume. This means that
code in finally blocks will not run, so if you have transaction oriented
cleanup in finally blocks this may not actually get executed. Appdomains do
not get unloaded.

Finalizers will still run, so resource-centric cleanup will still get done
(e.g. you can release unmanaged resources that are wrapped by a managed
object in the managed object's finalizer).

This shutdown is graceful in the sense that it is controlled by the clr, and
it is not a hard shutdown like killing the app, but if you need cleanup code
in finally blocks to run then this may not be suitable for you. You could
instead set a global shutdown flag/event that your main routine monitored
and used as a signal to exit.

Dave
 
Back
Top