How to abort TWeBrowser navigation progress?

3k Views Asked by At

Delphi 6

I've got code that loads a Webbrowser control (TEmbeddedWB) via a local HTML file. It works fine for the most part, and has for quite a few years and 1000's of users.

But there's a particular end-user page that has a script that does some sort of Google translate stuff, which makes the page take a really long time to load, upwards of 65 seconds.

I'm trying to make the webbrowser stop/abort/quit so that the page can be reloaded or so that the app can exit. However, I can't seem to make it stop. I've tried Stop, loading about:blank, but it doesn't seem to stop.

wb.Navigate(URL, EmptyParam, EmptyParam, EmptyParam, EmptyParam );
while wb.ReadyState < READYSTATE_INTERACTIVE do Application.ProcessMessages;

The app remains in the ReadyState loop (ReadyState = READYSTATE_LOADING) for quite a long time, upwards of 65 seconds.

Anyone have any suggestions?

1

There are 1 best solutions below

0
On

If you are using TWebBrowser then the TWebBrowser.Stop or if you want IWebBrowser2.Stop is the right function suited for this purpose. Try to do this little test and see if it stops the navigation to your page (if the navigation takes more about 100ms of course :)

procedure TForm1.Button1Click(Sender: TObject);
begin
  Timer1.Enabled := False;
  WebBrowser1.Navigate('www.example.com');
  Timer1.Interval := 100;
  Timer1.Enabled := True;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  if WebBrowser1.Busy then
    WebBrowser1.Stop;
  Timer1.Enabled := False;
end;

If you are talking about TEmbeddedWB then take a look at the WaitWhileBusy function instead of waiting for ReadyState change. As the only parameter you must specify the timeout value in milliseconds. Then you can handle the OnBusyWait event and interrupt the navigation if needed.

procedure TForm1.Button1Click(Sender: TObject);
begin
  // navigate to the www.example.com
  EmbeddedWB1.Navigate('www.example.com');
  // and wait with WaitWhileBusy function for 10 seconds, at
  // this time the OnBusyWait event will be periodically fired;
  // you can handle it and increase the timeout set before by
  // modifying the TimeOut parameter or cancel the waiting loop
  // by setting the Cancel parameter to True (as shown below)
  if EmbeddedWB1.WaitWhileBusy(10000) then
    ShowMessage('Navigation done...')
  else
    ShowMessage('Navigation cancelled or WaitWhileBusy timed out...');
end;

procedure TForm1.EmbeddedWB1OnBusyWait(Sender: TEmbeddedWB; AStartTime: Cardinal;
  var TimeOut: Cardinal; var Cancel: Boolean);
begin
  // AStartTime here is the tick count value assigned at the
  // start of the wait loop (in this case WaitWhileBusy call)
  // in this example, if the WaitWhileBusy had been called in
  // more than 1 second then
  if GetTickCount - AStartTime > 1000 then
  begin
    // cancel the WaitWhileBusy loop
    Cancel := True;
    // and cancel also the navigation
    EmbeddedWB1.Stop;
  end;
end;