Avatar of tondog
tondog
 asked on

In Delphi, Anyone know how to REALLY REALLY pause the program till the WebBrowser website is really really finished loading.

I have tried several ways to try to detect if the webbrowser is finished loading a website.

I can't seem to find a solution that always works though.

while WebBrowser1.ReadyState <> READYSTATE_COMPLETE do application.ProcessMessages;

This seems to work the best -- following the NAVIGATE command

But doesnt seem to work when i submit a form.

Anyone know a method to REALLY check if the webbrowser is still busy

p.s. I don't really care about flash or images loading.

thanks!
Editors IDEsDelphi

Avatar of undefined
Last Comment
8080_Diver

8/22/2022 - Mon
8080_Diver

Short answer?  "Yes."
8080_Diver

Sorry, couldn't resist that one. ;-)
Okay, here's what I have done with geat success:
 

procedure TfrmYourForm.PrepTheBrowser;
begin
  WebBrowser1.Navigate(CURRENTHTML);
  WaitDocComplete;
end;

procedure TfrmYourForm.WaitDocComplete;
begin
  try
    while (not DocComplete) and (not application.terminated) do
      Application.ProcessMessages;

    if not Application.Terminated then
    begin
      // Do Stuff like:	
      //    set interfaces
      //    set doc & frame index variables
    end;
  except
    on E:Exception do
      begin
        raise Exception.Create( #13#10 + 'ERROR: Unexpected Exception in ' +
                                'frmYourForm.WaitDocComplete; ' +
                                #13#10 + E.Message);
      end; {on exception}
  end; {try}
end;

Open in new window

ASKER CERTIFIED SOLUTION
twinsoft

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
tondog

ASKER
8080_Diver,

thanks but what is DocComplete ?
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
twinsoft

Hi, you can use something like this:


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, OleCtrls, SHDocVw, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    WebBrowser1: TWebBrowser;
    procedure Button1Click(Sender: TObject);
    procedure WebBrowser1DocumentComplete(Sender: TObject;
      const pDisp: IDispatch; var URL: OleVariant);
  private
    FLoading: Boolean;
  public
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
 WebBrowser1.Navigate('http://www.experts-exchange.com');
 Floading := True;
 while FLoading do Application.ProcessMessages;
 ShowMessage('Finished');
end;

procedure TForm1.WebBrowser1DocumentComplete(Sender: TObject;
  const pDisp: IDispatch; var URL: OleVariant);
var
  CurWebrowser: IWebBrowser;
  TopWebBrowser: IWebBrowser;
begin
  CurWebrowser := pDisp as IWebBrowser;
  TopWebBrowser := (Sender as TWebBrowser).DefaultInterface;
  FLoading := not (CurWebrowser = TopWebBrowser);
end;

end.

Open in new window

tondog

ASKER
This seemed to work good

though i have no idea why.

thanks!
8080_Diver

Oops,
Forgot one of the procedures.  This one is associated with the DocumentComplete Event.
DocComplete is a boolean that is used to flag the success.

procedure TfrmYourForm.WebBrowser1DocumentComplete(      Sender  : TObject;
                                                                  const pDisp   : IDispatch;
                                                                  var   URL     : OleVariant);
(******************************************************************************
 **                                                                          **
 ** Procedure:    WebBrowser1DocumentComplete                                **
 **                                                                          **
 ** Purpose:      This procedure                                             **
 **                                                                          **
 ** Parameters:   Sender - TObject - the calling object                      **
 **                                                                          **
 ** Exceptions:   If an error occurs during this routine's check for the     **
 **               OnDocumentComplete event passing the same Dispatch         **
 **               interface as the OnNavigateComplete event handler,         **
 **               then an exception is raised.                               **
 **                                                                          **
 ** Author:       Ralph D. Wilson II                                         **
 **                                                                          **
 ** Date Created: 2005-03-01                                                 **
 **                                                                          **
 ** Revision History                                                         **
 **     Date      Initials   Modification                                    **
 **  03/01/2005    RDWII     Initial code created.                           **                                                                          **
 ******************************************************************************)
begin
  try
    { Only the final OnDocumentComplete event passes the same
      Dispatch interface as the OnNavigateComplete event handler.}
    if (pDisp = CurDispatch)
    then begin
      DocComplete := True;
      CurDispatch := nil; {clear the global variable }
    end; {if}
  except
    on E:Exception do
      begin
        raise Exception.Create( #13#10 + 'ERROR: Unexpected Exception in ' +
                                'frmYourForm.WebBrowser1DocumentComplete; ' +
                                #13#10 + E.Message);
      end; {on exception}
  end; {try}
end;

Open in new window

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.