2012-01-23 28 views
3

德尔福6如何中止TWeBrowser导航进度?

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

但有具有脚本,做某种谷歌的翻译的东西,这使得网页需要很长的时间来加载,向上65秒的特定最终用户页面。

我试图让网页浏览器停止/退出/退出,这样的页面可以重新加载或使应用程序可以退出。但是,我似乎无法让它停下来。我试过停止,加载about:空白,但它似乎并没有停止。

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

该应用程序保持在readyState的环路(readyState的= READYSTATE_LOADING)相当长的一段时间,向上的65秒。

任何人有任何建议吗?

+0

当您使用停止方法时会发生什么? – EMBarbosa

+1

@TLama:我有同样的问题,你的解决方案并没有帮助...它仍然在'ReadyState = READYSTATE_LOADING'和'EmbeddedWB1.Stop;'没有帮助...... – Legionar

回答

3

如果您使用的是TWebBrowser,那么TWebBrowser.Stop或者如果您想要IWebBrowser2.Stop是适合此目的的正确功能。尝试做这个小测试,看看它是否停止导航到你的页面(如果导航过程中需要的:)更100ms左右

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;