尝试访问TWebbrowsers HTML时应用程序锁定 [英] Application locks while trying to access TWebbrowsers HTML

查看:146
本文介绍了尝试访问TWebbrowsers HTML时应用程序锁定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

修改
将其缩小到此1行

Edit Narrowed it down to this 1 line,

HTML := wb.OleObject.Document.documentElement.innerHTML;

消耗时间...如何加快?

which consumes the time... how can that be speed up?

使用以下代码,我的应用程序可以在尝试访问页面的HTML(Delphi XE)时挂起1-2秒。

Using the following code my application can hang for 1-2 seconds while it tries to access the HTML of a page (Delphi XE).

function Button1Click(Sender : TObject);
begin
   wb.navigate('http://10.0.0.154/stats');
   // Use a timer to poll the page - dont wait and process app messages
   timer1.enabled := true;
end;

procedure Timer1Timer(Sender : TObject);
var
  HTML : WideString;
begin
   If GetHTML(HTML) = true then
   begin
      Timer1.enabled := false;
      { do something }
   end;
end;


function GetHTML(var HTML : WideString) : boolean;
var
  Document : IHTMLDocument2;
begin
  HTML := '';
  Result := false;

  Document := wb.DOcument as IHTMLDocument2;
  If Assigned(Document) then
  begin
    try
      HTML := wb.OleObject.Document.documentElement.innerHTML;
      Result := true;
    except
      Result := false;
    end;
  end;
end;

然而,我注意到我的GetHTML方法可能需要1-2秒钟才能返回某些东西并锁定UI。用Delphi XE查看AQTime,它表示方法调用缓慢(1-2秒)。这是sporatic,我不知道如果页面仍然是中等负载失败。

However I notice in my GetHTML method can take 1-2 seconds to return something and it locks UI. Looking at the AQTime with Delphi XE it says that method call is slow (1-2 seconds). It's sporatic and I wonder if it fails when the page is still mid load.

我正在加载的页面是一个内部页面,充满了javascript和500k大,我可以不要使用OnDocumentComplete,因为它在页面甚至准备好之前触发,即使我在ReadyState上进行检查,它仍然会提前启动。

The page I am loading is an inhouse page, full of javascript and 500k big, I can't use the OnDocumentComplete because it fires before the page is even ready, even if I do a check on the ReadyState it still fires to early.

任何人都可以摆脱一些光,如果他们一个更快的方式我可以访问TWebbrowser的HTML?

Anyone able to shed some light, if their a faster way I can access the HTML of TWebbrowser?

推荐答案

请记住OnDocumentComplete可以在浏览页面时多次(框架)触发。

Please remember that OnDocumentComplete can fire multiple times (frames) when navigating a page.

如何正确实现OnDocumentComplete:

How to properly implement OnDocumentComplete:

procedure YourForm.OnDocumentComplete(
  Sender: TObject;
  const pDisp: IDispatch;
  var URL: OleVariant);
var
  currentBrowser: IWebBrowser;
  topBrowser: IWebBrowser;
  document: OleVariant;
  windowName: string;
begin
  currentBrowser := pDisp as IWebBrowser;
  topBrowser := (Sender as TWebBrowser).DefaultInterface;
  if currentBrowser = topBrowser then
    ShowMessage('Complete document was loaded')
  else
  begin
    document := currentBrowser.Document;
    windowName := document.ParentWindow.Name;
    ShowMessage(Format('Frame "%s" was loaded', [windowName]));
  end;
end;

来源:

http://www.cryer.co.uk/brian/delphi/twebbrowser/twebbrowser_events .htm#OnDocumentComplete

这篇关于尝试访问TWebbrowsers HTML时应用程序锁定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆