even more thread problems: error catching
so, heres my issue: i have a thread that runs a function to check a wireless modem to see if theres any new sms's. every thing seems to be going well but i need to plan for the eventuallity that someone will be unplug the modem. unfortunatly, i cant seem to catch the resulting error. heres what i got:
Code:
Public Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim j As Integer = 0
Dim i As Integer = 0
Debug.Print("bg worker begin")
While i = j
If BackgroundWorker1.CancellationPending Then
i = 2
e.Cancel = True
BackgroundWorker1.Dispose()
Debug.Print("bg worker middle")
Exit While
Else
CheckForNew()
End If
End While
Debug.Print("bg worker end")
End Sub
and heres the functio it calls:
Code:
Public Sub CheckForNew()
Debug.Print("checkfornew begin")
Dim storage = getMessageStorage()
Dim MsgLocation As Integer
Dim counter As Integer = 0
Try
If isPortAModem() = True Then
comm = New GsmCommMain(Microsoft.VisualBasic.Right(GetPort, 1))
comm.Open()
counter = 0
Try
Dim messages As DecodedShortMessage() = comm.ReadMessages(PhoneMessageStatus.ReceivedUnread, PhoneStorageType.Sim)
If (messages.Length.ToString()) = 0 Then
Else
End If
For Each DecodedShortMessage In messages
MsgLocation = DecodedShortMessage.Index
ShowMessage(DecodedShortMessage.Data)
counter = counter + 1
comm.DeleteMessage(MsgLocation, PhoneStorageType.Sim)
Next
Catch ex As IOException
MsgBox(ex.Message)
Finally
End Try
Try
comm.Close()
Catch ex As IO.IOException
End Try
Else
End If
Debug.Print("chekcfornew end")
Catch ex As Exception
End Try
End Sub
this function is called every second for the durration of the programs life (it check to new txt messages). i beleive the error is comming from the .open command. i also believe that because its on a different thread, the try..catch statemenst arent working properly (am i right?). the errorr thats not being caught can only be seen durring debug (durring run time of the application, the program just crashes with no error).
how do i catch this
Re: even more thread problems: error catching
do you know what type of exception is being thrown? You are catching IOException only, so if the exception is of some other type, or even of the base exception type, you won't catch it.
I would try changing your code to "Catch Ex as Exception" instead, even if its just to test so you can see the exact type of exception being thrown, and then you can catch that one specifically too.
You do have a regular catch ex as exception at the very outside of your code, which should catch the exception if it bubbles up that far, however you also don't actually handle it, you just catch it, so the app is basically just swallowing any exception at that point and acting like it ran fine...
Try/Catch blocks can have multiple catch statements.
Code:
Try
'code here
Catch Ex as IOException
'handle IO Exception
Catch Ex as Exception
'handle any other exception
End Try
Re: even more thread problems: error catching
yeah, it is an ioerror but ive tried using just "capture ex as exception". it gets by anyway. could it be be cause its running in a seperate thread?
Re: even more thread problems: error catching
I don't think so really.. your error handling is in the same routine that is in the seperate thread, so it should work.
If you take out all your error handling, do you see the exact place the code hits the exception?
You could also try adding try/catch blocks in the actual DoWork routine, and see if you get any different behavior then.
Re: even more thread problems: error catching
yeah, thats another problem. when the error pops, it doesnt hi light the part of code thats causing the error, as it normally does.