tags:

views:

46

answers:

2

Hey Everyone,

I have a login dialog being displayed in a WPF application that has a cancel button on it that instead attempting to log into the application, closes it down. The problem is, I have a user control that appears on the mainwindow form, so if I attempt to run the Application.Current.Shutdown() or this.Close() methods, I get an exception from one of the event handlers assigning an event to a specific control on the user control. The exception is a NullReferenceException, i.e, object reference not set to instance of an object.

From my limited understanding of WPF, it would appear as if, while I'm attempting the shut the application down, there is another thread (the UI thread?) attempting to initialise the components on MainWindow. I'm not sure whether or not this is correct, but I was hoping someone here could shed light on why its doing this and any means of stopping it so I can shut the application down properly.

Further information, this login dialog and the subsequent Application.Current.Shutdown() method is all taking place within the MainWindow contructor, dunno if that helps.

Thanks for the help.

A: 

I suspect that you do not remove event handlers before disposing main window, that might cause this exception.

Vitalij
public event RoutedEventHandler NewModelClick{ add { TopMenu.NewModelClick += value; } remove { TopMenu.NewModelClick -= value; }}
Walter Sharp
it throws the exception on add{ TopMenu.NewModelClick += value; } line, TopMenu is NULL
Walter Sharp
@Walter: please Edit your original question with this info. Things like this tend to get lost in the Comments section, especially in regard to an original question. To do so, select "Edit" under your post above, and paste the info in there. Be sure to highlight your code when you paste it in, and select the "Code" button (the one with the 1's and 0's) in order for the code to show up as formatted code.
Wonko the Sane
A: 

Okay nevermind, I got the answer, I placed the InitialiseComponent() method around an if statement and set the bool to false if the user clicked exit, Happy days :)

Walter Sharp
If You show the dialog box during the component initialisation, you could just call return after you dialog box closes to prevent application initialising the rest of the window.
Vitalij