.NET Framework Developer Center >
.NET Development Forums
>
Windows Presentation Foundation (WPF)
>
WPF application crashes when Window is programmatically closed in constructor (with .Net Framework 3.0 SP2)
WPF application crashes when Window is programmatically closed in constructor (with .Net Framework 3.0 SP2)
- Hi All,
I have a WPF application and in the constructor i am trying to close the window using this.Close() and return (in certain scenarios like login fail etc.). Some thing like the below
if(loginFail)
{
this.Close();
return;
}
This was working fine. But recently after upgrading the .Net Framework 3.0 with SP2 (by means of installing .Net 3.5 SP1), the application started crashing whenever it tries to programatically close and return in the constructor. It gives an InvalidOperationException. It seems somewhere the SetVisibility of the window is called by the framework even though it is closed and returned in constructor itself. With .Net 3.0 SP1, it was not crashing; but with SP2 it seems probably the order of window creation and application running is altered a bit.
Could you please help me in resolving this issue?
Thanks and Regards,
Manikandan
All Replies
- Hi Manikandan1,
-->With .Net 3.0 SP1, it was not crashing; but with SP2 it seems probably the order of window creation and application running is altered a bit.
I am sorry I do not find a machine with .NET Framework 3.0 SP2 installed for now, but my working machine has installed .NET Framework 3.5 and get no exception when running the code. Which line of code throws the expection? At "this.Close()" or "return"? Since you have closed the current window, I think there is no need to call "return".
Thanks.
Sincerely.
Jim Zhou -MSFT - Hi Jim,
First of all sorry for my late reply and thanks for your reply.
---> I am sorry I do not find a machine with .NET Framework 3.0 SP2 installed for now, but my working machine has installed .NET Framework 3.5 and get no exception when running the code.
It will crash after you upgrade the .net framework 3.5 to .net framework 3.5 SP1 (which in turn would also upgrade the .net 3.0 and .net 2.0 if any to SP2).
--->Which line of code throws the expection? At "this.Close()" or "return"?
It is the subsequent operations (I persume in the application class ) after return that throws the exception.
--->Since you have closed the current window, I think there is no need to call "return".
Actually we have put a lot of operations in the constrcutor as a part of initiallizing the application. AFAI remember, we didn't want those code snippets to be executed once the window is closed, so a return statement is placed there. Do you mean the subsequent initialization code in the constrcutor wont be executed once the window is closed in the constrcutor itself?
Looking forward for your reply.
Thanks and Regards,
Manikandan - Hi Manikandan1,
-->I am sorry I do not find a machine with .NET Framework 3.0 SP2 installed for now
I noticed a tiny typo in my previous post, my .NET Framework version is 3.0 SP1, not SP2, sorry for possible confusion.
-->Do you mean the subsequent initialization code in the constrcutor wont be executed once the window is closed in the constrcutor itself
No, even if you call this.Close method in the constructor of window, the subsequent initialization code will also be executed as expected, after the control is out of the block of constructor, the current window will close immediately.
Thanks.
Sincerely.
Jim Zhou -MSFT - Hi Manikandan,
Personally, I do not think it's a good idea to just close/return like that from a constructor.
Might I suggest replacing your constructor with a factory method?
public class App { ... public static App Create(...) { ... if (loginFail) { return null; } ... return new App(); } } App app = App.Create(...); if (app!=null) { app.Show(); } else { // Failed to create app // Error handling code in here }
Regards,
Min Chew- Marked As Answer byJim Zhou - MSFTModeratorWednesday, November 11, 2009 8:50 AM
- Unmarked As Answer byJim Zhou - MSFTModeratorWednesday, November 18, 2009 6:36 AM


