mercredi 6 mai 2015

VB.NET best practice for applications running for a long time?

I have made an application in vb.net that can sometimes take several hours to finish up, and i have noticed that it will quite often crash while waiting for some processes to finish.

For example:

My app uses Form.Show() to open 3 other forms that are needed in the process of the app. These forms can sometimes take several hours to finish, while they are running, my mainform is inactive, and just waiting for form.formclosing() events to happen, at which point it will then execute clean up codes and exit the app. My problem is, that when the forms that my maincode spawns closes, it sometimes crashes the app with a "System.Reflection.TargetInvocationException" error in the windows log files. I know where it crashes, i just dont know how to prevent it from crashing at this point.

So on mainform.load() i have addhandlers for the forms:

AddHandler Form1.FormClosing, AddressOf Form1Closed
AddHandler Form2.FormClosing, AddressOf Form2Closed

And Boolean Dims for later:

Dim F1Closed As Boolean = False
Dim F2Closed As Boolean = False

The app then displays the forms:

Public sub StartForms()
    form1.Show()
    form2.Show()
end sub

and thats the end of that sub, and the mainform then goes on standby until:

Public Sub Form1Closed()

        F1Closed = True

        If F2Closed = True Then
            CloseApp()
        End If

    End Sub
Public Sub Form2Closed()

    F2Closed = True

    If F1Closed = True Then
        CloseApp()
    End If

End Sub

And my close code:

Public Sub CloseApp()
    Try

        For i As Integer = System.Windows.Forms.Application.OpenForms.Count - 1 To 1 Step -1

            Dim form As Form = System.Windows.Forms.Application.OpenForms(i)

            form.Dispose()
        Next i
    Catch ex As Exception
    Finally
        Me.Dispose()

        Environment.Exit(0)
    End Try
End Sub

During the Form1.FormClosing or Form2.FormClosing, the app just crashes with the invocation error as above.

I cant get any more detail on it, even with a try..catch in the events.

Any help would be much appreciated..

Aucun commentaire:

Enregistrer un commentaire