HTMLviewer Slow

I have this program for Windows I’m working on. I’m grabbing the source from the HTMLViewer then looping through it until the page is loaded. I split the text into an array and then search the array strings to parse the data. Everything works but it is slow and I’m not sure what is slowing it down. Is it the HTMLviewer? I wouldn’t think it is the do while loop since I exit out of it after I see the page is loaded. After it runs I have a label that gets populated by the word “complete” and then a msgbox that says “done”. The label updates instantly but the msgbox does not fire until about 2 seconds after, leading me to believe the main thread is still running something. Below is a skeleton of my code. As mentioned the code works it is just slow. I removed some of the text since it wasn’t important.

---start do loop
do until racefound = "yes" 
 txtSource.Text = ""
  dim b as ChromiumBrowserMBS = HTMLViewer1.ChromiumBrowserMBS
  if b<>Nil then
    dim c as ChromiumFrameMBS = b.mainFrame
    if c<>Nil then
      dim t as string = c.Source
      txtSource.Text = t
    end if
  end if
  
  lstSourceAr.RemoveAllRows
  
  'check for page loaded by getting active track
  re.SearchPattern = "span class=""name""|""details"""
  oMatch = re.Search(txtSource.value)
  while oMatch isA RegExMatch 
    fullMatchStr = oMatch.SubExpressionString(0)
    if instr(fullMatchStr,"class=""name""") > 0 then
      ---page is loaded
      racefound = "yes"
    else
      s = fullMatchStr
      lstSourceAr = s.Split("<div ")
      exit
    end if
    oMatch = re.Search
  wend
Loop
----end loop

---parse array
for i = 0 to lstSourceAr.LastIndex
  if instr(lstSourceAr(i),"id=""type") > 0 then
    description = ....
  elseif instr(lstSourceAr(i),"id=....... then
    description = ....
  elseif instr(lstSourceAr(i),"id=....... then
    description = ....
  elseif instr(lstSourceAr(i),"id=....... then
    description = ....
  elseif instr(lstSourceAr(i),"id=....... then
   description = ....
  elseif instr(lstSourceAr(i),"id=....... then
    description = ....
  elseif instr(lstSourceAr(i),"id=....... then
    description = ....
  elseif instr(lstSourceAr(i),"id=....... then
    description = ....
  elseif instr(lstSourceAr(i),"id=....... then
   description = ....
  elseif instr(lstSourceAr(i),"id=....... then
   description = ....
  end if
next

---done parsing show complete
lblRF.Text = "Complete"

---done show msgbox, this takes 2 seconds after the lblRF.Text is updated with "Complete".
msgbox("Done")

Please remember to put your code in a code block (as I just did for you) using the </> button

This would benefit from some RegEx, which @Kem_Tekinay is an expert with.

You might want to do this differently. The way this code is written it could pull the source of the htmlviewer hundreds of times per second.

Instead of a while loop, how about using a timer?

1 Like

Thanks for the response. I’ve done similar things with a timer, I just figured the while loop would be faster since as soon as it finds the match it will exit, is that not the case? Does the while loop continue to process after it exits? When racefound = “yes” in the loop I had a label that was updated so I knew when the loop found the searchpattern. It updated quickly but the msgbox still didn’t fire for 2 seconds after. I guess to minimize processing time do I avoid a loop? As I mentioned in the loop it finds the pattern, updates the label and exits the loop but something is still making it take 2 more seconds after.

Are you talking about using RegEx instead of the instr() on the arrays?

Sorry, disregard. I see you’re using RegEx to narrow. Don’t know how I missed it before.

It seems (from what I can tell) the issue is with the page not being completely loaded. I used a timer instead of the do while loop and the processing time was the same. I guess the issue is that even though I can see the data in the browser the source is not completely loaded. It takes the extra 2 seconds to completely load the source and then my RegEx finds the searchpattern and the rest of the code finishes quickly. I guess there isn’t much I can do about the page source not being completely loaded.

my guess is that the thing you are looking for is only added to the page after some post-processing is done but before the DocumentLoaded javascript event fires. I’m afraid there’s not much you can do about that.

Thanks Greg, I’m almost sure that is what is happening. The page is loaded dynamically after the user selection so that is what is happening. Thanks for the help I just wanted to cancel out any do while loop issues.