Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / VB

Web Browser Hangs/Freeze Issue VB.NET

5.00/5 (2 votes)
30 Oct 2014CPOL 15K   290  
The tip is for those who getting application hang/freeze issue in web browser control.

Introduction

This tip is for solution of web browser control hangs/freeze application issue. This is useful to those who used web browser control for scraping or visiting dynamic link, etc.

Background

When scraping website for visiting multiple links dynamically in a little time or in milliseconds/seconds, sometimes scripts of that page need more time to load content and in between you try to visit another page that hangs/freezes your application and crashes it. Most common error is "Out of memory".

Using the Code

Code for Visiting URL/ Navigation
VB.NET
WebBrowser1.ScriptErrorsSuppressed = True
WebBrowser1.Url = New Uri(URL)
WebBrowser1.AllowNavigation = True 

Line 1: "ScriptErrorsSuppressed" hides all its dialog boxes that originate from the underlying ActiveX control & script errors.

Line 2: Will visit/navigate to given URL.

Line 3: Will allow URL to navigate if denied.

Create Web Browser Navigating Event
VB.NET
Private Sub webBrowser1_Navigating(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.WebBrowserNavigatingEventArgs) Handles WebBrowser1.Navigating
    If (e.Url.ToString().Contains("about:blank")) Then
        WebBrowser1.Stop()
        WebBrowser1.AllowNavigation = False
        Return
    End If
End Sub  

Will stop browser while navigating the same URL multiple times for loading scripts and deny navigation.

Create WebBrowser Navigated Event
VB.NET
Private Sub webBrowser1_Navigated(ByVal sender As Object, _
    ByVal e As WebBrowserNavigatedEventArgs) _
    Handles WebBrowser1.Navigated
    WebBrowser1.AllowNavigation = True
End Sub 

Will allow navigation.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)