Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop executing C# code

Tags:

c#

winforms

wpf

I'm playing a little bit with some C# Winforms/WPF code and just stumbled upon something strange. Let's say I have a code like this:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DoSomething();

        // something more if everything worked okay
    }
}

What puzzles me is that I cannot simply close the application from the method DoSomething before the constructor finishes its job. If anything during the execution of DoSomething fails, I need to close the application immediately; however, it just keeps running, executes the part // something more... and THEN closes, but that's way too late for me.

I have to put the code for closing the form inside the constructor itself with a following return; and then it works, but I don't really find that an acceptable solution. I'm trying to move such validation logic from the constructor to my methods.

I've tried things like:

public void DoSomething()
{
    Close();
}

and

public void DoSomething()
{
    Application.Current.Shutdown();
}

But it doesn't seem to work. Yes, both codes do close the application, but only after a fully finished constructor code.

Why would I need such a thing? Well, because at startup I need to check for various things, like availability of the connection and hardware, validate the user etc, and if anything fails, there's no point of executing more code.

I tried the same principle with Winforms and WPF (hence the tags) — works the same way.

Can anybody provide an explanation or a solution?

like image 273
walther Avatar asked Nov 10 '12 09:11

walther


1 Answers

Just try using Environment.Exit(-1) in your situation and all will be good.

ADDED: This is the best reference i can get for you.

Difference between Application.Exit vs Application.Shutdown vs Environment.Exit

Application.Exit() is for exiting a windows forms application in a graceful way. Basically, it stops the message pump, closes all windows and lands you back in the Main() method just after the call to Application.Run(). However, sometimes it doesn't appear to work - this is usually because there are other foreground threads (apart from the UI thread) still running which are preventing the thread from ending.

Application.Shutdown() is (broadly) the equivalent of Application.Exit() in a WPF application. However, you have a bit more control as you can set the ShutDownMode so that the application shuts down when the main window closes, the last window closes or only when this method is called.

Environment.Exit() kills all running threads and the process itself stone dead. This should only be used in WF or WPF as a last resort when the more graceful methods are not working for some reason. It can also be used to make an abrupt exit from a console application.

Another Reference: How to properly exit a C# application?

like image 126
Furqan Safdar Avatar answered Nov 14 '22 22:11

Furqan Safdar