Share via

doEvenys ???

Devon Nullman 80 Reputation points
2026-03-24T00:03:54.55+00:00

I need to parse and extract information from over 500 .html log files.

Fnames is a list of the .html filenames

at the start

Where is the folder and searchtype is top directory only

Fnames.AddRange(My.Computer.FileSystem.GetFiles(where, SearchType, "*.html"))

    For X = 0 To Fnames.Count - 1 

Load each file:

        WebBrowser1.Navigate(Fnames(X))

        Do

            Application.DoEvents()

        Loop Until WebBrowser1.ReadyState = WebBrowserReadyState.Complete

    Next

If I don't have "Application.DoEvents()", the app freezes

If I use threading.thread.sleep(any value from 1 to 1000), the app freezes

If I do have the doEvents() - all is well but I heard that it should never be used

The app has two buttons - Start and Save, I disable both when processing starts and enable them when processing ends

All of the data extraction takes place in the DocumentCompleted function

The only issue is waiting for the url to fully load.

Developer technologies | VB

Answer accepted by question author
  1. Nancy Vo (WICLOUD CORPORATION) 1,905 Reputation points Microsoft External Staff Moderator
    2026-03-24T06:29:28.56+00:00

    Hi @Devon Nullman ,

    Thanks for your question.

    Removing DoEvents causes a freeze and a simple Thread.Sleep does not help because you are repeatedly "pumping" the Windows message queue. This lets the operating system process all the pending events for the WebBrowser control and also keeps your form responsive.

    I suggest removing the entire For-Next loop with the DoEvents waiting and let the DocumentCompleted event handle everything automatically.

    1. Add a variable to remember which file you're currently processing.
    2. When the Start button is clicked, prepare the list of files, disable the buttons, and load only the first file.
    3. Move all your data extraction code into the DocumentCompleted event.
    4. After extracting data from the current file, automatically load the next one — and so on until all files are done.

    This is code example you can refer to:

    • Add these at the top of your form:
    Private CurrentIndex As Integer = 0
    Private Fnames As New List(Of String)
    
    • When the Start button is clicked:
        Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
            Dim where As String = "C:\Users\Nancy\source\repos\TestHTMLLogs\"
            Dim SearchType As Microsoft.VisualBasic.FileIO.SearchOption = Microsoft.VisualBasic.FileIO.SearchOption.SearchTopLevelOnly
    
            Fnames.Clear()
            Fnames.AddRange(My.Computer.FileSystem.GetFiles(where, SearchType, "*.html"))
    
            If Fnames.Count = 0 Then
                MessageBox.Show("No HTML files found in the folder.")
                Return
            End If
    
            CurrentIndex = 0
            btnStart.Enabled = False
            btnSave.Enabled = False
    
    
            WebBrowser1.Navigate(Fnames(CurrentIndex))
        End Sub
    
    • Now update your DocumentCompleted event:
        Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
            If e.Url.AbsoluteUri <> WebBrowser1.Url.AbsoluteUri Then Exit Sub
            If WebBrowser1.ReadyState <> WebBrowserReadyState.Complete Then Exit Sub
    
            ' === YOUR DATA EXTRACTION CODE GOES HERE ===
            MessageBox.Show("Finished processing file " & (CurrentIndex + 1) & " of " & Fnames.Count & vbCrLf & Fnames(CurrentIndex), "File Loaded")
    
            ' TODO: Put your real data extraction code here
    
            CurrentIndex += 1
    
            If CurrentIndex < Fnames.Count Then
                WebBrowser1.Navigate(Fnames(CurrentIndex))
            Else
                btnStart.Enabled = True
                btnSave.Enabled = True
                MessageBox.Show("Successfully processed all " & Fnames.Count & " HTML files.", "Done!")
            End If
        End Sub
    

    I tested this approach on my side and it worked.

    I hope this addresses your question. If this response was helpful, please consider following the guidance to provide feedback


1 additional answer

Sort by: Most helpful
  1. Q&A Assist
    2026-03-24T00:04:04.6666667+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    Application.DoEvents is required in the current design because the UI thread is blocked in a tight loop while waiting for WebBrowser1.ReadyState to become Complete. Without yielding, the message pump does not run, so the WebBrowser control cannot finish navigation and the form appears frozen.

    DoEvents works here because it:

    • Yields execution so Windows can process pending UI and control events.
    • Returns control after the OS has processed the event queue and SendKeys queue.

    However, DoEvents is generally discouraged because:

    • It can re-enter event procedures while they are already running, causing unpredictable behavior.
    • Other applications or parts of the same app can interact with the UI in unexpected ways while control is yielded.

    From the documentation:

    • DoEvents “yields execution so that the operating system can process other events.”
    • It is “most useful for simple things like allowing a user to cancel a process after it has started.”
    • For long‑running work, a better approach is to use a Timer or move the work to a separate component so the UI thread is not blocked.

    In this scenario, a more robust pattern is:

    • Avoid the busy Do … Loop Until WebBrowser1.ReadyState = Complete on the UI thread.
    • Start navigation and let the DocumentCompleted event drive the workflow (e.g., queue of URLs, navigate to the next file from DocumentCompleted).
    • Or use asynchronous patterns (Async/Await) and non‑UI HTML parsing instead of a WebBrowser control.

    If the current design is kept, Application.DoEvents is the reason the app does not freeze, but it should be used with care because of the re‑entrancy and unpredictability described in the documentation.


    References:

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.