Home » WPF

An unhandled exception of type 'System.StackOverflowException' occurred in WindowsBase.dll

i have .net 4.0 application in wpf . application is running fine but when i close the application by using exit button. it throws the exception after stopping the debugging. the exception is  "An unhandled exception of type 'System.StackOverflowException' occurred in WindowsBase.dll ".

the code in exit button is


 private void btnExit_Click(object sender, RoutedEventArgs e)


don't know from where it throws the exception and why ??


5 Answers Found


Answer 1

Hello Usman,

is the exit  button in your main window? If so, you could try to call

instead of Environment.Exit(0), and check if the exception  still occurs.



Answer 2

Thank for quick response Niehaus

yes niehaus exit  button is in main window. but if use this.close() than application  closes but the VS reamins in the debugging  mode. and i have threads in my main window so i have to stopp the debugging mode which i seems only possible with environmnet.exit. what do u think ?


Answer 3

Hi Usman,

you'll have to assure that all threads have been ended before shutting down your application. As to shutting down from the main window, I'd recommend closing all windows and have those windows themselves be responsible for cleaning up any potentially used resources resp. ending any running  threads. In order to loop through all open windows in order to close  them, have a look at my WindowHelpers blog post - the CloseAllWindows method is what you will want to use for that (see the Closing all Windows section).


Answer 4

olaf i have used this method its ok for thread but still vs not coming out of debugging  mode with this.close() call. but with enviornment.exit()
it came out from debugging mode but gives the exception  on some time on closing application. could any body tell me why windowsbase.dll throws  exception and any work around to handle this exception

Answer 5

Hi Usman,

as I said, there'll be something that has not been disposed of appropriately which, in turn, prevents your running  EXE from shutting down. This doesn't have to do anything with Visual Studio or its debug-mode though. Open up the bin/debug-folder in Windows Explorer and run the compiled executable directly; after ending the program, open up Task Manager and you should see your EXE in the processes list.

As for what might be causing the stack overflow exception  - this could easily occur if there was a recursive or reentrant call to a method, i.e. in your DisconnectReaders method. Have you tried setting a breakpoint and stepping through your code  line by line?



<< Previous      Next >>

Microsoft   |   Windows   |   Visual Studio   |   Sharepoint   |   Azure