Hey I have a program that has a webBrowser navigate through a few pages, and when it gets to the last page I set it to navigate to, checks if there is a certain string on that page. I just switched my code around to use multithreading and now the statement

If WebBrowser1.DocmentText.Contains("...") Then
...
End If

generates an InvalidCastException that says "Specified Cast is Not Valid". Anybody have an idea of why this might be happening.

Here is how I start the thread (when a button is clicked)

FUBCThread.Start()

And here is the Sub that is run

Public Sub beginFUBC()
        While True
            If TimeOfDay > "11:31:00 PM" Or TimeOfDay < "07:00:00 AM" Then
            Else
                WebBrowser1.Navigate(...)
                Delay(10)
                WebBrowser1.Navigate(...)
                Delay(5)
                WebBrowser1.Navigate(...)
                Delay(5)
                WebBrowser1.Navigate(...)
                Delay(5)
                WebBrowser1.Navigate(...)
                Delay(5)

                Try
                    If WebBrowser1.DocumentText.Contains("...") Then
                        SendSMTP()
                        Exit Sub
                    End If
                Catch x As Exception
                        MessageBox.Show(x.Message)
                End Try
            End If

            Dim time As Integer = Convert.ToInt32(timeBox.Text)
            Threading.Thread.Sleep((time * 60) * 1000) 'Sleeps for however many minutes specified in the time box minutes and then tries again
        End While
    End Sub

Recommended Answers

All 6 Replies

Any ideas?

Member Avatar for Unhnd_Exception

I think your problem was the document wasn't complete when check the string.

I put your string checking in the documentComplete event of the webbrowser.

Added a global suppressEvent so the event isn't handled until you navigated to the last site.

Added a global boolean documentComplete so the app can hang until your code in the document complete event is completed.

Private suppressEvent As Boolean
Private documentcomplete As Boolean

Public Sub beginFUBC()              
   While True
        If TimeOfDay > "11:31:00 PM" Or TimeOfDay < "07:00:00 AM" Then
        Else
            'suppress all document complete events until the last navigation
            suppressEvent = true
            WebBrowser1.Navigate(...)
            Delay(10)
            WebBrowser1.Navigate(...)
            Delay(5)
            WebBrowser1.Navigate(...)
            Delay(5)
            WebBrowser1.Navigate(...)
            Delay(5)
            
            'Allow the document complete event to process and set the document complete status to false so we can wait on it.
            suppressEvent = false
            documentComplete = false

            WebBrowser1.Navigate(...)
            Delay(5)
            
            'this will hang until documentcomplete is done processing.
            Do While documentcomplete = False
                Threading.Thread.Sleep(100)
                Application.DoEvents()
            Loop

       End If

       Dim time As Integer = Convert.ToInt32(timeBox.Text)
       Threading.Thread.Sleep((time * 60) * 1000) 'Sleeps for however many minutes specified in the time box minutes and then tries again
    End While
End Sub

Private Sub WebBrowser1_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted

    If suppressEvent Then Exit Sub 'Only process when told to


    Try
        If WebBrowser1.DocumentText.Contains("...") Then
            SendSMTP()
            Exit Sub
        End If
    Catch x As Exception
        MessageBox.Show(x.Message)
    End Try

    'Inform the app the doc is complete and suppress the event
    suppressEvent = True
    documentcomplete = True
End Sub

I'll try that when I get back to my computer but I don't think that's the problem. It was the first thing I thought of but instead of using your method, I set the delay after the last navigate to 1 minute (more than enough time for the document to finish loading) and the exception was still thrown. Do you think that your method will get different results than just waiting until the document is for sure finished loading?

Member Avatar for Unhnd_Exception

When I run your code I get the exception.

When I check it in the document complete event I don't get the exception.

Ok so I will definitely try that when I get to my computer

Alright, that worked thanks. Would you happen to know the specific reason that the delay wouldn't work, or why this occurred only after I switched the program to use multithreading?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.