By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,882 Members | 2,453 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,882 IT Pros & Developers. It's quick & easy.

Application.Exit() problem

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Are you calling your Check_For_Dir() Sub from within the Sub New by any
chance? If so, Application.Exit won't work.
"Mike Johnson" <jo*****@bellsouth.net> wrote in message
news:O9**************@TK2MSFTNGP11.phx.gbl...
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

Nov 21 '05 #2

P: n/a
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"
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

Nov 21 '05 #3

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

--
Jonathan Allen
"Mike Johnson" <jo*****@bellsouth.net> wrote in message
news:O9**************@TK2MSFTNGP11.phx.gbl...
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

Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.