3

德尔福6

我有通过本地 HTML 文件加载 Webbrowser 控件 (TEmbeddedWB) 的代码。它在大多数情况下都可以正常工作,并且已经使用了好几年和 1000 多个用户。

但是有一个特定的最终用户页面有一个脚本,该脚本执行某种谷歌翻译内容,这使得页面需要很长时间才能加载,超过 65 秒。

我正在尝试使网络浏览器停止/中止/退出,以便可以重新加载页面或退出应用程序。但是,我似乎无法让它停下来。我试过停止,加载 about:blank,但它似乎并没有停止。

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

应用程序在 ReadyState 循环 (ReadyState = READYSTATE_LOADING) 中保持相当长的时间,超过 65 秒。

有人有什么建议吗?

4

1 回答 1

3

如果您正在使用TWebBrowserTWebBrowser.Stop或者如果您想要IWebBrowser2.Stop,则适合此目的的正确功能。尝试做这个小测试,看看它是否会停止导航到您的页面(如果导航需要更多大约 100 毫秒当然:)

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;

如果您正在谈论,TEmbeddedWB请查看WaitWhileBusy功能而不是等待ReadyState更改。作为唯一参数,您必须以毫秒为单位指定超时值。然后您可以处理该OnBusyWait事件并在需要时中断导航。

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;
于 2012-01-23T20:30:12.317 回答