具有“指定强制转换无效”的Vb多线程错误 [英] Vb multithreading with “specified cast is not valid” error

查看:75
本文介绍了具有“指定强制转换无效”的Vb多线程错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好..

如果我在没有线程的情况下运行下面的代码,那么一切都应该如此,但是当我以新线程启动时,我会收到System.InvalidCastException错误并显示已指定演员无效



预先感谢您提供任何帮助。




  Dim  thread1  As  线程( AddressOf  th10)

私有 Sub Form3_Load(发件人作为 对象, e As EventArgs)句柄 MyBase 。载荷
CheckForIllegalCrossThreadCalls = False
Button1.PerformClick()
结束 Sub

私有 Sub Button1_Click(发件人作为 对象,e As EventArgs)句柄 Button1.Click
thread1.Start()
结束 Sub

公共 Sub th10()
WebBrowser3.Navigate( https:// www .somewebsite.com
WebBrowser3.ReadyState<> WebBrowserReadyState.Complete
Application.DoEvents()
结束

Dim pos4 作为
Dim bp2 作为 字符串

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

pos4 = InStr(pos4 + 1 ,value1, sometextfromwebsite,vbTextCompare)
bp2 = value1.Substring(pos4)
Dim startIndex1 作为 整数 = 17
Dim length1 作为 整数 = 16
Dim substring1 As 字符串 = bp2.Substring(startIndex1,length1)

TextBox3.Text = substring1
结束 Sub





我有什么试过:



此错误显示在While WebBrowser3.ReadyState ...以及我将其更改为;



 静态 start 作为  
start = VB.Timer()
VB.Timer()< start + 5
Application.DoEvents()
Loop





(我不喜欢指定页面加载时间的想法,在某些情况下可能需要更长时间)



......似乎没问题,但是我在Dim value1 As String = WebBrowser3.Document.Body.InnerText

解决方案

错误报告!

您忘记了URL末尾的引用

 公共  Sub  th10()
WebBrowser3.Navigate( https://somewebsite.com
' ^缺失引用此处



此代码无效。


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"

解决方案

Bug report !
you forgot a quote at the end of URL

Public Sub th10()
       WebBrowser3.Navigate("https://somewebsite.com")
       '                                            ^ missing quote here


this code can't have worked.


这篇关于具有“指定强制转换无效”的Vb多线程错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆