An object-oriented programming language developed by Microsoft that can be used in .NET.
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.
- Add a variable to remember which file you're currently processing.
- When the Start button is clicked, prepare the list of files, disable the buttons, and load only the first file.
- Move all your data extraction code into the DocumentCompleted event.
- 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