Application.Exit() problem

M

Mike Johnson

I have the following code in form1 and when the application starts this sub
is called to check for a path if not found a message box is displayed and
then gives the user an option to end the application but when this option is
selected the application does not end but continues to the next line of code
which tries to setup a filewatcher with the path which does not exist, which
then gives an error. please help.

Public Sub Check_For_Dir()

Dim MyPath, MyName As String

Dim Count As Integer

Dim Result As DialogResult

Count = 0

MyPath = FilePath ' Set the path.

MyName = Dir(MyPath, FileAttribute.Directory) ' Retrieve the first entry.

Do While Count <> 10000

If MyName = "" Then ' Start the loop.

Count = Count + 1

MyName = Dir(MyPath, FileAttribute.Directory) 'Check again

Else

Exit Do

End If

Loop

If Count = 10000 Then

'Displays Message

Result = MessageBox.Show("Path " + FilePath + " not available, press Yes to
try again or No to Exit.", "Error", MessageBoxButtons.YesNo)

If Result = DialogResult.Yes Then

'Run Sub again

Check_For_Dir()

Else

'Hide the tray icon.

'NotifyIcon.Visible = False

'Close up.

Me.Close()

Application.Exit()

End If

End If

End Sub
 
D

Dr Screwup

Are you calling your Check_For_Dir() Sub from within the Sub New by any
chance? If so, Application.Exit won't work.
 
C

Cor Ligthert

Mike,

I changed your code a little in this snippet bellow, your code looks a
little bit outdated with declaring all variables in top and than using the
do while where an for index is very simple to use.

\\\
For i As Integer = 0 To 9999
If MessageBox.Show("Path", "Error", _
MessageBoxButtons.YesNo) = DialogResult.Yes Then
'do your stuff
Else
Me.Close()
Exit Sub
End If
Next
///

(As extra advice, paste next time your code in a notebook first and copy it
back and then into the message will give you probably more answers, than it
will be more readable, now it is difficult to get a quick overview)

I hope this helps?

Cor

"Mike Johnson"
 
J

Jonathan Allen

1. Use a sub main that calls Application.Run if you find all your files.
This avoids the need to use Applciation.Exit.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top