这是我的代码
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'BackgroundWorker1.RunWorkerAsync()
myFunction()
End Sub
Private Sub myFunction()
WebBrowser1.Navigate("http://google.com")
While WebBrowser1.ReadyState <> WebBrowserReadyState.Complete
Application.DoEvents()
End While
TextBox1.Text = WebBrowser1.Document.Body.OuterText.ToString
End Sub
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
myFunction()
End Sub
当我运行它并单击button1时它工作正常,但是当我将button1更改为:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
BackgroundWorker1.RunWorkerAsync()
End Sub
我收到此错误消息“指定的演员表无效”
我的目标很简单
1.单击按钮加载页面
2.等待页面完全加载
3.然后抓住页面的outerText
答案 0 :(得分:1)
与已经建议的一样,您应该使用网络浏览器控件DocumentCompleted
event来确定网页何时完全加载。 Application.DoEvents()
is bad,不应该用来保持用户界面的响应能力!
我不知道你的代码最初是从哪里来的,也不知道为什么这么多人使用它,但它是 BAD PRACTICE!
正确的方法是使用return redirect()->guest('auth/login');
事件:
DocumentCompleted
另外,跳过Private Sub myFunction()
WebBrowser1.Navigate("http://google.com")
End Sub
Private Sub WebBrowser1_DocumentCompleted(sender As System.Object, e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
If WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then
TextBox1.Text = WebBrowser1.Document.Body.OuterText 'No need to call ToString() since 'OuterText' already is a string.
End If
End Sub
。无论如何都无法调用UI线程,无法从后台线程访问BackgroundWorker
。
巧合的是,几个小时前我回答了a similar question ......