2012-01-23 30 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;