如何终止TWebBrowser的导航进度?

3

Delphi 6

我有一段代码,它通过本地HTML文件加载Webbrowser控件(TEmbeddedWB)。大部分情况下都可以正常工作,而且已经被成千上万个用户使用了好几年。

但是,有一个特定的终端用户页面包含一个脚本,用于进行某种谷歌翻译操作,导致该页面加载时间特别长,高达65秒。

我试图让Webbrowser停止/中止/退出,以便重新加载页面或使应用程序退出。然而,我似乎无法让它停止。我尝试过Stop、加载about:blank,但它似乎仍在运行。

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

该应用程序在ReadyState循环中保持(ReadyState = READYSTATE_LOADING)相当长的时间,高达65秒。
有没有建议?

当您使用stop方法时会发生什么? - EMBarbosa
1
@TLama:我也有同样的问题,你提供的解决方案没有帮助到我...它仍然停留在“ReadyState = READYSTATE_LOADING”,而“EmbeddedWB1.Stop;”也没有起作用... - Legionar
1个回答

3
如果你正在使用 TWebBrowser,那么TWebBrowser.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;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接