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.
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;
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”