gpt4 book ai didi

delphi - 如何中止 TWeBrowser 导航进度?

转载 作者:行者123 更新时间:2023-12-03 15:55:07 26 4
gpt4 key购买 nike

德尔福6

我有通过本地 HTML 文件加载 Web 浏览器控件 (TMembeddedWB) 的代码。它在大多数情况下运行良好,并且已经拥有多年的用户和数千名用户。

但是有一个特定的最终用户页面有一个脚本,可以执行某种Google翻译功能,这使得该页面需要很长时间才能加载,最多 65 秒。

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

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

应用程序会在 ReadyState 循环 (ReadyState = READYSTATE_LOADING) 中停留相当长的时间,长达 65 秒。

大家有什么建议吗?

最佳答案

如果您使用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;

关于delphi - 如何中止 TWeBrowser 导航进度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8976933/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com