Skip to content

WebView2 Control Process and State Management

Process Management

BrowserProcessId Property

  • Description: Gets the ProcessID of the browser process
  • Type: Long
  • Example:
vb
' Get and display browser process ID
Private Sub ShowBrowserProcessInfo()
    Dim pid As Long
    pid = WebView21.BrowserProcessId
    Debug.Print "Browser process ID: " & pid
End Sub

ProcessFailed Event Handling

  • Description: Handles WebView2-related process failures
  • Example:
vb
Private Sub WebView21_ProcessFailed(ByVal Kind As wv2ProcessFailedKind)
    Select Case Kind
        Case wv2ProcessFailedKind.wv2ProcessFailedKindBrowserProcessExited
            ' Browser process exited
            HandleBrowserCrash
            
        Case wv2ProcessFailedKind.wv2ProcessFailedKindRenderProcessExited
            ' Render process exited
            HandleRenderProcessCrash
            
        Case wv2ProcessFailedKind.wv2ProcessFailedKindFrameRenderProcessExited
            ' Frame render process exited
            HandleFrameProcessCrash
    End Select
End Sub

Private Sub HandleBrowserCrash()
    ' Log error
    LogError "Browser process crashed"
    
    ' Try to recover
    If MsgBox("Browser process has terminated. Reload page?", _
              vbQuestion + vbYesNo) = vbYes Then
        WebView21.Reload
    End If
End Sub

Private Sub HandleRenderProcessCrash()
    ' Log error
    LogError "Render process crashed"
    
    ' Display error page
    WebView21.LoadHtml "<html><body>" & _
                      "<h1>Page Rendering Failed</h1>" & _
                      "<p>Please refresh the page to try again.</p>" & _
                      "</body></html>"
End Sub

Suspend and Resume

Suspend/Resume Support Check

  • Description: Checks if suspend and resume features are supported
  • Property: SupportsSuspendResumeFeatures As Boolean
  • Example:
vb
' Check if suspend/resume features are supported
Private Function CanUseSuspendFeatures() As Boolean
    CanUseSuspendFeatures = WebView21.SupportsSuspendResumeFeatures
End Function

Suspend Method

  • Description: Suspends WebView2 processing and rendering
  • Purpose: Used for implementing tab functionality to reduce resource usage for inactive tabs
  • Example:
vb
' Suspend current tab
Private Sub SuspendTab()
    If WebView21.SupportsSuspendResumeFeatures Then
        WebView21.Suspend
    End If
End Sub

Resume Method

  • Description: Resumes previously suspended WebView2 processing and rendering
  • Example:
vb
' Resume suspended tab
Private Sub ResumeTab()
    If WebView21.SupportsSuspendResumeFeatures Then
        WebView21.Resume
    End If
End Sub

IsSuspended Property

  • Description: Gets whether WebView2 is in a suspended state
  • Type: Boolean
  • Example:
vb
' Check and handle suspended state
Private Sub HandleTabActivation()
    If WebView21.SupportsSuspendResumeFeatures Then
        If WebView21.IsSuspended Then
            ' Tab is activated, resume processing
            WebView21.Resume
        End If
    End If
End Sub

Complete Tab Management Example

vb
' Tab type definition
Private Type TabInfo
    WebView As WebView2
    Caption As String
    IsSuspended As Boolean
End Type

' Tab collection
Private Tabs() As TabInfo
Private ActiveTabIndex As Long

' Switch to specified tab
Private Sub SwitchToTab(ByVal Index As Long)
    If Index < 0 Or Index > UBound(Tabs) Then Exit Sub
    
    ' Suspend current active tab
    If ActiveTabIndex >= 0 And ActiveTabIndex <= UBound(Tabs) Then
        With Tabs(ActiveTabIndex)
            If Not .IsSuspended And .WebView.SupportsSuspendResumeFeatures Then
                .WebView.Suspend
                .IsSuspended = True
            End If
            .WebView.Visible = False
        End With
    End If
    
    ' Activate new tab
    With Tabs(Index)
        If .IsSuspended And .WebView.SupportsSuspendResumeFeatures Then
            .WebView.Resume
            .IsSuspended = False
        End If
        .WebView.Visible = True
        .WebView.SetFocus
    End With
    
    ActiveTabIndex = Index
    UpdateTabButtons
End Sub

' Create new tab
Private Sub CreateNewTab(ByVal Url As String)
    ' Expand array
    ReDim Preserve Tabs(UBound(Tabs) + 1)
    Dim newIndex As Long
    newIndex = UBound(Tabs)
    
    ' Create new WebView2 control
    With Tabs(newIndex)
        Set .WebView = Controls.Add("WebView2Control.WebView2", "tab" & newIndex)
        
        ' Configure WebView2
        With .WebView
            .Move TabContainer.Left, TabContainer.Top, _
                  TabContainer.Width, TabContainer.Height
            .IsStatusBarEnabled = False
            .Navigate Url
        End With
        
        .Caption = "New Tab"
        .IsSuspended = False
    End With
    
    ' Switch to new tab
    SwitchToTab newIndex
End Sub

' Close tab
Private Sub CloseTab(ByVal Index As Long)
    If Index < 0 Or Index > UBound(Tabs) Then Exit Sub
    
    ' Remove WebView2 control
    Controls.Remove Tabs(Index).WebView.Name
    
    ' Reorganize array
    For i = Index To UBound(Tabs) - 1
        Tabs(i) = Tabs(i + 1)
    Next i
    
    ReDim Preserve Tabs(UBound(Tabs) - 1)
    
    ' If closing current tab, switch to another tab
    If Index = ActiveTabIndex Then
        If UBound(Tabs) >= 0 Then
            SwitchToTab IIf(Index > 0, Index - 1, 0)
        End If
    ElseIf Index < ActiveTabIndex Then
        ActiveTabIndex = ActiveTabIndex - 1
    End If
    
    UpdateTabButtons
End Sub

This example demonstrates how to combine suspend/resume functionality to implement a complete multi-tab browser. It includes:

  • Tab data structure
  • Tab switching logic
  • New tab creation functionality
  • Tab closing functionality
  • Resource management optimization

VB6 and LOGO copyright of Microsoft Corporation