Rol*_*son 21 delphi multithreading delphi-2007 twebbrowser
我使用TWebBrowser来显示Google地图.问题是它在加载地图时会阻塞主ui线程.是否可以在单独的线程中更新地图?
编辑: RRUZ你是对的TWebBrowser有URL的异步加载.但是我发现了阻止呼叫的问题:
if WaitWhileProcessing and (MapQueryResult.Count > 0) then
Result := MapQueryResult[0] as TMapQuery;
Run Code Online (Sandbox Code Playgroud)
和方法:
function TMapItemCollection.WaitWhileProcessing: Boolean;
var
vMaxSleepCnt: Integer;
begin
Result := True;
vMaxSleepCnt := 0;
while Processing or Loading do
begin
inc(vMaxSleepCnt);
Application.ProcessMessages;
Sleep(100);
if vMaxSleepCnt = 100 then
begin
Result := False;
Break;
end;
end;
end;
Run Code Online (Sandbox Code Playgroud)
所以它似乎解决了这个代码应该重构.但这不是这个问题的范围.
当执行 if 语句并调用 WaitWhileProcessing 来评估条件时,它会循环 100 次,并有十分之一秒的睡眠时间。但是调用 ProcessMessages 时有哪些消息在等待呢?是否可以再次递归调用该方法?它永远不会进入睡眠状态,但会不断调用此方法。顺便说一句,请注意 ProcessMessages 确实是不好的做法,但现在......尝试这个:
var
isWaitWhileProcessingBusy :boolean = false;
function TMapItemCollection.WaitWhileProcessing: Boolean;
var
vSleepCnt: Integer;
begin
if not isWaitWhileProcessingBusy then
begin
isWaitWhileProcessingBusy = true;
vSleepCnt := 0;
while Processing or Loading or vSleepCnt < 100 do
begin
inc(vSleepCnt);
Application.ProcessMessages;
Sleep(100);
end;
isWaitWhileProcessingBusy := false;
end;
Result = Processing or Loading;
end;
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,我还改变了其他一些小事情。中断不在 while 条件中,结果只是处理或加载的结果(因为该表达式给出了实际结果)。函数外部额外的 isWaitWhileProcessingBusy 可防止消息循环重新进入。希望这能防止锁定用户界面。这也不是最佳实践,但目前它可能有助于解决并查明问题。
您轮询“加载/处理”是否有原因?使用TWebBrowser的OnDocumentComplete事件不是更容易吗?
...另一个想法闪过我的脑海...你检查过任务管理器吗?谷歌地图使用 Flash,这是一个也使用主 UI 线程的 ActiveX 组件。这也可能是导致饥饿的资源消耗。
祝你好运!