An unhandled exception of type 'System.StackOverflowException'...

Coleman34

Member
Joined
Mar 10, 2015
Messages
5
Programming Experience
1-3
I am having a small issue, well its not small, its probably a total PITA! I re-coded a windows based form, and when I try and close the form i get the error

"An unhandled exception of type 'System.StackOverflowException' occurred in System.Windows.Forms.dll"

The debug points me at the last line of this code. Can anyone show me how to either elegantly handle this or trouble shoot it?
VB.NET:
   Private Sub frmGetUserInput_FormClosing(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing


        Dim cancel As Boolean = eventArgs.Cancel
        Dim unloadMode As System.Windows.Forms.CloseReason = eventArgs.CloseReason


        If ((unloadMode = 0) And CurrentlyWorking) Then ' user tried to cancel process by closing the progress window (upper right "X")
            lblThisReportStatus.Text = "Cancelling..."
            worker.cancel = True
            cancel = 1 ' prevent from closing right now (give time for process to be canceled properly)
        Else
            If (Not worker Is Nothing) Then
                worker.Close() ' the program does not terminate until the child form is unloaded
            End If


            Me.Close()


        End If


        eventArgs.Cancel = cancel


    End Sub
 
This code should be the problem, calling Close from Closing event will cause an infinite number of calls to that event handler, which will cause stack to overflow:
Me.Close()
Also, why are you setting/comparing Boolean types and enumeration types with numbers?
cancel = 1
Here is how you assign a Boolean value:
cancel = True

unloadMode = 0
Here is how to compare with a CloseReason value:
unloadMode = CloseReason.None

User closing is CloseReason.UserClosing by the way.

Also a few comment about this:
Private Sub frmGetUserInput_FormClosing(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Since you use .Net 4.5 it would seem you have copied this code from somewhere or spent time modifying it yourself,
  • event parameters is by default 'sender' and 'e', this is a specific design guideline from Microsoft for all events, they have never been 'eventSender'/'eventArgs',
  • they are also ByVal by default and since VS 2010 they are no longer added in generated event handler signatures,
  • Forms namespace should also be unnecessary to specify in a form.
If you let VS generate FormClosing event handler today it will look like this:
Private Sub frmGetUserInput_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing

About closing, if you just need to close a child form close it and let main closing continue. If you need to stop closing, then stop it (cancel) and inform user what is happening, then either let user perform the necessary steps and close again, or let the child closing handler continue closing the app after cleanups. It should be as tidy and quick as possible, especially if user or system want the app to close now. Since you are setting a 'cancel' flag on child form it may indicate some background thread is running that you wish to cancel gracefully first, that could also be instructed to close app when it has had time to cancel. Background processing could also in some cases be forcefully terminated, where you with a Finally part in handled exception code block would perform cleanups. There could be many considerations, and not possible to give a 'do this' answer to all.
 
Thanks

You are right, the code that was Quoted was brought forward from some old vb6 code that I did not create, nor do I fully understand the mindset of the creator because he is no longer around to ask. So I am muddling through his old code. I was able to bring it forward enough for the forms to properly launch, but they do not interact with excel the way they should...but I think you for your help.
 
Back
Top