|
-
Jul 4th, 2006, 10:58 AM
#1
Thread Starter
Addicted Member
[02/03] Threading and Web Exception
Hey. I have a question on aborting a thread and error messages here. I have a thread that is used to do the following: creates a temporary directory on the c:\ drive, checks to see if a text file is available on my server, and downloads it if available. What I've created here is an updating system for my program basically. I have a STOP button that lets the user stop the process and close the updating forum. Here's some of my code:
VB Code:
Private Sub startUpdating()
ButtonForceUpdate.Visible = False
If findPaths() = True Then
If findDefinitions() = True Then
If downloadDefinitions() = True Then
startUIThread = True 'used to tell UI thread that it can check versions now
'must now use timer to check instead of using delegate code
Else
Me.Close()
End If
Else
Me.Close()
End If
Else
Me.Close()
End If
End Sub
Private Function findPaths() 'finds temp download paths
LabelChecking.Text = "Creating Temporary Directories..."
Try
IO.Directory.CreateDirectory("C:\tempupdater")
tempDIR = "C:\tempupdater"
Return True
Catch ex As Exception
MsgBox(ex.Message)
Return False
End Try
End Function
Private Function findDefinitions() 'find definitions on server
LabelChecking.Text = "Locating Definitions on Server..."
Try
wc.OpenRead("http://www.myserver.com/version.txt")
Return True
Catch ex As Exception 'cant find definition
If MsgBox("The definition files couldn't be found on the server. Would you like to try again?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
findDefinitions() 'try again if click yes
Else
Return False
End If
End Try
Private Function downloadDefinitions()
LabelChecking.Text = "Downloading Definitions..."
Try
wc.DownloadFile("http://www.myserver.com/version.txt", tempDIR & "definitions.txt")
Return True
Catch ex As Exception
If MsgBox("An error occured while downloading definitions. The following error was returned:" & vbNewLine & vbNewLine & ex.Message & vbNewLine & vbNewLine & " Would you like to try and download again?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
downloadDefinitions()
Else
Return False
End If
End Try
End Function
Private Sub formUpdate_Closing(ByVal sender As Object, ByVal ByVale As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
Try
IO.File.Delete(tempDIR & "definitions.txt")
IO.Directory.Delete(tempDIR, True)
Catch ex As Exception
End Try
startUpdatingThread.Abort()
Me.Dispose()
fu = Nothing
End Function
ok so that's basically all my code that's in a thread. I apologize in advance, because I'm new to programming so it might not be correct. Ok the problem is that when the user wants to stop updating, then it will give me an error. For example if the user is currently in the stage for downloading the definitions, it will throw the exception there about not being able to download them. So it actually confuses the user because they clicked stop. How can I make it so that it will just stop with no errors and what not? Any ideas?
Thanks
John
-
Jul 6th, 2006, 12:09 PM
#2
Thread Starter
Addicted Member
Re: [02/03] Threading and Web Exception
-
Jul 6th, 2006, 02:07 PM
#3
Re: [02/03] Threading and Web Exception
How are you cloasing the thread? Just thread.abourt?
TPM
Add yourself to the VBForums Frappr Map!!
-
Jul 6th, 2006, 05:10 PM
#4
Thread Starter
Addicted Member
Re: [02/03] Threading and Web Exception
yes like in the .closing method for the forum. Is it not good programming practice to abort a thread when it's not done or something? That's basicalyl what I'm trying to do. Just stop the thread when the forum is done and it should stop everything in the background; but I end up getting an exception in whichever method the thread is currently running.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|