Click here to Skip to main content
15,917,565 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all..
If I run the code below without threading everything works like it should, but when I start it as a new thread then I receive ‘System.InvalidCastException’ error with “Specified cast is not valid”

Thanks in advance for any help you can provide.


VB
Dim thread1 As New Thread(AddressOf th10)

Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    CheckForIllegalCrossThreadCalls = False
    Button1.PerformClick()
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    thread1.Start()
End Sub

Public Sub th10()
    WebBrowser3.Navigate("https://www.somewebsite.com")
    While WebBrowser3.ReadyState <> WebBrowserReadyState.Complete
        Application.DoEvents()
    End While

    Dim pos4 As Long
    Dim bp2 As String

    Dim value1 As String = WebBrowser3.Document.Body.InnerText

    pos4 = InStr(pos4 + 1, value1, "sometextfromwebsite", vbTextCompare)
    bp2 = value1.Substring(pos4)
    Dim startIndex1 As Integer = 17
    Dim length1 As Integer = 16
    Dim substring1 As String = bp2.Substring(startIndex1, length1)

    TextBox3.Text = substring1
End Sub


What I have tried:

This error shows at “While WebBrowser3.ReadyState…” and when I change it to;

VB
Static start As Single
start = VB.Timer()
Do While VB.Timer() < start + 5
Application.DoEvents()
Loop


(I don’t like the idea to specify a time for page load, in some cases it might take longer)

…it seems to be fine, but then I receive the same error on “Dim value1 As String = WebBrowser3.Document.Body.InnerText”
Posted
Updated 27-Aug-16 10:59am
v5

1 solution

Bug report !
you forgot a quote at the end of URL
VB
Public Sub th10()
       WebBrowser3.Navigate("https://somewebsite.com")
       '                                            ^ missing quote here

this code can't have worked.
 
Share this answer
 
v4
Comments
Patrice T 27-Aug-16 16:58pm    
You have just done the correction.
Jack-tek460 27-Aug-16 16:58pm    
Thanks, now I see. Updated my question

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900