与WebBrowser.IsBusy或readyState的(VB .NET)InvalidCastException的 [英] InvalidCastException with WebBrowser.IsBusy or ReadyState (VB .NET)

查看:227
本文介绍了与WebBrowser.IsBusy或readyState的(VB .NET)InvalidCastException的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被建议作为一个答案另一个我的问题,该方法(<一打转转href=\"http://stackoverflow.com/questions/1731752/automate-website-log-in-and-form-filling\">http://stackoverflow.com/questions/1731752/automate-website-log-in-and-form-filling),并发现了一些奇怪的。

回答上述问题是使用一系列的javascript以便填写网络表格并提交,它调用的URL。我一直在努力,但没有成功一个VB .NET程序内自动完成这一工作。

我得到了最初的例子不工作,presumably,因为你是在同一个线程等待作为其中W​​ebBrowser控件工作:

  WebBrowser1.Navigate时(http://www.google.com)
做虽然WebBrowser1.IsBusy OrElse运算WebBrowser1.ReadyState&LT;&GT; WebBrowserReadyState.Complete
    Threading.Thread.Sleep(1000)
Application.DoEvents()
循环
WebBrowser1.Navigate时(JavaScript的:函数%20F(){document.forms [0] ['Q']值='计算器';} F();)
Threading.Thread.Sleep(2000年)等运行JavaScript
WebBrowser1.Navigate时(JavaScript的:document.forms [0] .submit())
Threading.Thread.Sleep(2000年)等运行JavaScript

如果你不等待的话,那也不行,当然。您最初浏览到的URL被中断。但有趣的是,你不能执行导航,以刻不容缓的JavaScript调用,无论是。

所以,我已经尝试了两种其他方法:使用DocumentCompleted事件等待浏览巢URL直到浏览器完成加载页面。不幸的是,DocumentCompleted并不总是火,似乎并没有每个JavaScript网址后开火。

我试过的第二种方法是把在一个单独的线程等待:

 私人代表小组SetTextDelegate(BYVAL TheText作为字符串)
 私人小组delSetText(BYVAL TheText作为字符串)
     WebBrowser1.Navigate时(TheText)
 结束小组 私人小组BrowseTo(BYVAL URL作为字符串)
     如果WebBrowser1.InvokeRequired然后
         Me.BeginInvoke(新SetTextDelegate(AddressOf delSetText),URL)
     其他
         WebBrowser1.Navigate时(URL)
     万一
 结束小组 私人小组TargetURL中()
   BrowseTo(http://www.google.com)
 结束小组 私人小组TypeSomethingIn()
     BrowseTo(JavaScript的:函数%20F(){document.forms [0] ['G'] =值'测试';} F();)
 结束小组 私人小组SubmitForm()
     BrowseTo(JavaScript的:document.forms [0] .submit())
 结束小组 私人小组等待()
     虽然真
         如果WebBrowser1.ReadyState = WebBrowserReadyState.Complete然后退出小组
         Threading.Thread.Sleep(100)
     虽然结束
 结束小组 私人小组自动浏览()
     TargetURL中()
     等待()
     TypeSomethingIn()
     等待()
     提交表格()
 结束小组 私人小组的button1_Click(BYVAL发件人为System.Object的,BYVAL E上System.EventArgs)把手Button1.Click
     昏暗T作为Threading.Thread
     T =新Threading.Thread(AddressOf自动浏览)
     t.Start()
 结束小组

好奇的是,readyState的在等待循环的检查(或IsBusy,对于这个问题)有时会抛出一个InvalidCastException的。 presumably调用这些都不是线程安全的?我不知道。如果我把有问题的呼叫尝试块内,等待循环,只不过没有工作。事实上,它甚至显得异常坚持搞砸了一切,因为即使通过code与try块的Visual Studio步进冻结换好10〜20秒(它确实没有try块相同)。

任何想法?


解决方案

  

一个我为我经验丰富,其中最有趣的问题
  没能找到INET的解决方案 - 是问题涉及到
  WebBrowser控件。问题是,当我试图访问
  WebBrowser控件实例文档属性,我得到
  无效的转换异常。问题是,WebBrowser控件是
  设计在一个线程工作。因此,要解决这个问题,你必须只检查
  InvokeRequired属性,如果它的值是true,则调用逻辑
  从委托,定成browser.Invoke(...)方法。


来源

I was playing around with the method that was suggested as an answer to another of my questions (http://stackoverflow.com/questions/1731752/automate-website-log-in-and-form-filling), and noticed something curious.

The answer to the above question was to use a series of javascript calls as URL's in order to fill in a web form and submit it. I have been trying to do this automatically inside a VB .NET program with no success.

The original example I was given doesn't work, presumably because you are waiting on the same thread as that in which the WebBrowser control is working:

WebBrowser1.Navigate("http://www.google.com")
Do While WebBrowser1.IsBusy OrElse WebBrowser1.ReadyState <> WebBrowserReadyState.Complete
    Threading.Thread.Sleep(1000)
Application.DoEvents()
Loop
WebBrowser1.Navigate("javascript:function%20f(){document.forms[0]['q'].value='stackoverflow';}f();")
Threading.Thread.Sleep(2000) 'wait for javascript to run
WebBrowser1.Navigate("javascript:document.forms[0].submit()")
Threading.Thread.Sleep(2000) 'wait for javascript to run

If you don't wait at all, it doesn't work either, of course. The URL you originally browse to is interrupted. But interestingly, you cannot perform the "navigations" to the javascript calls without delay, either.

So I've tried two other methods: using the DocumentCompleted event to wait to browse to the nest URL until the browser has finished loading the page. Unfortunately, DocumentCompleted does not always fire, and doesn't seem to fire after each javascript URL.

The second method I tried was to put the wait in a separate thread:

 Private Delegate Sub SetTextDelegate(ByVal TheText As String)
 Private Sub delSetText(ByVal TheText As String)
     WebBrowser1.Navigate(TheText)
 End Sub

 Private Sub BrowseTo(ByVal URL As String)
     If WebBrowser1.InvokeRequired Then
         Me.BeginInvoke(New SetTextDelegate(AddressOf delSetText), URL)
     Else
         WebBrowser1.Navigate(URL)
     End If
 End Sub

 Private Sub TargetURL()
   BrowseTo("http://www.google.com")
 End Sub

 Private Sub TypeSomethingIn()
     BrowseTo("javascript:function%20f(){document.forms[0]['g'].value='test';}f();")
 End Sub

 Private Sub SubmitForm()
     BrowseTo("javascript:document.forms[0].submit()")
 End Sub

 Private Sub Wait()
     While True
         If WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then Exit Sub
         Threading.Thread.Sleep(100)
     End While
 End Sub

 Private Sub AutoBrowse()
     TargetURL()
     Wait()
     TypeSomethingIn()
     Wait()
     SubmitForm()
 End Sub

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
     Dim t As Threading.Thread
     t = New Threading.Thread(AddressOf AutoBrowse)
     t.Start()
 End Sub

The curious thing is that the check of ReadyState (or IsBusy, for that matter) in the wait loop will sometimes throw a InvalidCastException. Presumably calls to these are not thread safe? I have no idea. If I put the offending call inside a Try block, the wait loop just fails to work. In fact, it even seems the exception "persists" to screw everything up, because even stepping through the code with the try block Visual Studio freezes for a good 10 to 20 seconds (it does the same without the try block).

Any ideas?

解决方案

One of the most interesting issues I had experienced and which for I was not able to find a solution in inet - was problem related to WebBrowser control. The thing is that when I was trying to access the Document property of the WebBrowser control instance, I was getting "Invalid cast exception". The thing is that the WebBrowser control is designed to work in one thread. So to fix this you must only check the InvokeRequired property and if it's value is true, then call the logic from the delegate, given into browser.Invoke(...) method.

Source

这篇关于与WebBrowser.IsBusy或readyState的(VB .NET)InvalidCastException的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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