Application.Exit() problem post number #2 I've made the correction suggested.

  • Thread starter Thread starter Mike Johnson
  • Start date Start date
M

Mike Johnson

The sub is being called from the Sub New(). If I can't use
Application.Exit() in this situation then how do I exit the application?
please help.

Public Sub Check_For_Dir()
Dim MyPath, MyName As String
MyPath = FilePath ' Set the path.
MyName = Dir(MyPath, FileAttribute.Directory) ' Retrieve the first
entry.
For i As Integer = 0 To 9999
If MessageBox.Show("Path " + FilePath + " not available, press
Yes to try again or No to Exit.", "Error", _
MessageBoxButtons.YesNo) = DialogResult.Yes Then
'Run Sub again
Check_For_Dir()
Else
Me.Close()
Exit Sub
End If
Next
End Sub
 
Set Form Visible = False then put Check_For_Dir() in then Form Load event.
If everything is OK the set Visible = True.

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles MyBase.Load
Check_For_Dir()
Me.Visible = True
End Sub


Public Sub Check_For_Dir()
Dim MyPath, MyName As String
MyPath = FilePath ' Set the path.
MyName = Dir(MyPath, FileAttribute.Directory) ' Retrieve the first
entry.
For i As Integer = 0 To 9999
If MessageBox.Show("Path " + FilePath + " not available, press
Yes to try again or No to Exit.", "Error", _
MessageBoxButtons.YesNo) = DialogResult.Yes Then
'Run Sub again
Check_For_Dir()
Else
Application.Exit
Exit Sub
End If
Next

End Sub
 
Mike Johnson said:
The sub is being called from the Sub New(). If I can't use
Application.Exit() in this situation then how do I exit the application?
please help.

Throw an Exception.
This will give you a nasty run-time error dialog rattling on about
an unhandled exception - no surprise, there; you just threw one!

To make this a little cleaner, start you program from Sub Main
(needs a little bit more code to get the Form's message loop up
and running), but you can catch the Exception, ignore it and let the
program quietly die, as in

Sub Main
Try
' Run the application's main form.
Application.Run(New Form1())

Catch ex as Exception
' Do Nothing
' - Or, maybe, give the User a nicer error

End Try

' Reaching here ends the program.
End Sub

HTH,
Phill W.
 
Back
Top