none
在新线程中创建的Webbrowser为什么不能响应事件呢 RRS feed

  • 问题

  • 代码如下,创建一个新的工程,然后直接使用下面的代码,会发现WebForm的mWebbrowser虽然定义为withEvents,但是仍然不能触发DocumentComplete事件,各位大侠,望不吝赐教,谢谢啊:)

    Public Class Form1
    
        Dim TempWebForm As WebForm
        Dim ThreadOfBackgroundWaiter As Threading.Thread
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ThreadOfBackgroundWaiter = New System.Threading.Thread(New System.Threading.ThreadStart(AddressOf BackgroundWaiter))
            ThreadOfBackgroundWaiter.Name = "后台等待线程"
            ThreadOfBackgroundWaiter.SetApartmentState(Threading.ApartmentState.STA)
            ThreadOfBackgroundWaiter.Start()
    
        End Sub
        Private Sub BackgroundWaiter()
            TempWebForm = New WebForm()
            TempWebForm.Visible = False
            TempWebForm.mWebBrowser.Navigate("http://www.163.com")
    
        End Sub
    End Class
    
    Public Class WebForm
        Inherits Form
    
        Public WithEvents mWebBrowser As WebBrowser
    
        Public Sub New()
            mWebBrowser = New WebBrowser()
            mWebBrowser.IsWebBrowserContextMenuEnabled = False
            mWebBrowser.ScriptErrorsSuppressed = False
            mWebBrowser.WebBrowserShortcutsEnabled = False
            mWebBrowser.Parent = Me
            mWebBrowser.Dock = DockStyle.Fill
        End Sub
    
        Private Sub mWebBrowser_DocumentCompleted(sender As Object, e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles mWebBrowser.DocumentCompleted
            Dim a = 1
        End Sub
    End Class
    


    编程是永无止境的,向大家学习

    2013年1月30日 9:45

答案

  • 问题的原因在于,网页还没来得及触发DocumentCompleted事件,窗口所在的线程就已经结束了,随之结束了窗体已经窗体的所有控件

    Public Class Form1
        Private Updateing As Boolean
        Dim ThreadOfBackgroundWaiter As Threading.Thread
        Public WithEvents mWebBrowser As WebBrowser
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ThreadOfBackgroundWaiter = New System.Threading.Thread(New System.Threading.ThreadStart(AddressOf BackgroundWaiter))
            ThreadOfBackgroundWaiter.Name = "后台等待线程"
            ThreadOfBackgroundWaiter.SetApartmentState(Threading.ApartmentState.STA)
            ThreadOfBackgroundWaiter.Start()
        End Sub
        Private Sub BackgroundWaiter()
            Updateing = True
            mWebBrowser = New WebBrowser()
            mWebBrowser.IsWebBrowserContextMenuEnabled = False
            mWebBrowser.ScriptErrorsSuppressed = False
            mWebBrowser.WebBrowserShortcutsEnabled = False
            'TempWebForm.Show()
    
            mWebBrowser.Navigate("http://www.baidu.com")
            Do While Updateing
                Application.DoEvents()
            Loop
        End Sub
    
        Private Sub mWebBrowser_DocumentCompleted(sender As Object, e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles mWebBrowser.DocumentCompleted
            Updateing = False
        End Sub
    End Class
    


    2013年1月31日 6:51

全部回复

  • 问题的原因在于,网页还没来得及触发DocumentCompleted事件,窗口所在的线程就已经结束了,随之结束了窗体已经窗体的所有控件

    Public Class Form1
        Private Updateing As Boolean
        Dim ThreadOfBackgroundWaiter As Threading.Thread
        Public WithEvents mWebBrowser As WebBrowser
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ThreadOfBackgroundWaiter = New System.Threading.Thread(New System.Threading.ThreadStart(AddressOf BackgroundWaiter))
            ThreadOfBackgroundWaiter.Name = "后台等待线程"
            ThreadOfBackgroundWaiter.SetApartmentState(Threading.ApartmentState.STA)
            ThreadOfBackgroundWaiter.Start()
        End Sub
        Private Sub BackgroundWaiter()
            Updateing = True
            mWebBrowser = New WebBrowser()
            mWebBrowser.IsWebBrowserContextMenuEnabled = False
            mWebBrowser.ScriptErrorsSuppressed = False
            mWebBrowser.WebBrowserShortcutsEnabled = False
            'TempWebForm.Show()
    
            mWebBrowser.Navigate("http://www.baidu.com")
            Do While Updateing
                Application.DoEvents()
            Loop
        End Sub
    
        Private Sub mWebBrowser_DocumentCompleted(sender As Object, e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles mWebBrowser.DocumentCompleted
            Updateing = False
        End Sub
    End Class
    


    2013年1月31日 6:51
  • 感谢分享!

    Shanks Zen
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2013年2月1日 8:19
    版主