我正在使用Skype4COM控件。不过,我的程序正试图使用For循环从我的联系人列表中删除大约3K联系人。
1)这需要很长时间
2)它可能会崩溃,"MyApp已停止工作“
我的猜测是,不知何故,我需要“放慢”我正在做的事情。
我会用睡眠();?因为我不确定这是否也会“暂停”Skype和我的程序之间的连接。
概括地说:我正在做一个有大量条目的动作,因为有了这个巨大的加载,我的程序挂了很长时间,最终会崩溃(有时)。有什么办法可以防止这种情况吗?
顺便说一下,Skype4COM是STA。
发布于 2011-03-19 20:38:37
将处理移动到单独的线程中。你的问题似乎是Windows认为应用程序已经停止响应,因为它没有处理它的消息循环。
调用Application.ProcessMessages是错误的解决方案,因为它所做的事情比您想象的要多得多。你可能会在重入的过程中遇到问题,或者发生一些你意想不到的事情。
确保线程在创建COM对象之前调用CoInitialize,并在创建COM对象时调用CoUnitialize。您可以找到在线程这里中使用COM的示例;本文提到了ADO,但演示了CoInitialize/CoUninitialize的使用。
编辑:在评论之后,我添加了一个在Delphi应用程序中接收自定义消息的示例。线程将需要访问UM_IDDELETED常量;您可以(最好)将它添加到单独的单元中,并在主窗体的单元和线程的单元中使用该单元,或者简单地在两个单元中定义它。
// uCustomMsg.pas
const
UM_IDDELETED = WM_APP + 100;
// Form's unit
interface
uses ..., uCustomMsg;
type
TForm1=class(TForm)
// ...
private
procedure UMIDDeleted(var Msg: TMessage); message UM_IDDELETED;
//...
end;
implementation
procedure TForm1.UMIDDeleted(var Msg: TMessage);
var
DeletedID: Integer;
begin
DeletedID := Msg.WParam;
// Remove this item from the tree
end;
// Thread unit
implementation
uses
uCustomMsg;
// IDListIdx is an integer index into the list or array
// of IDs you're deleting.
//
// TheFormHandle is the main form's handle you passed in
// to the thread's constructor, along with the IDList
// array or list.
procedure TYourThread.Execute;
var
IDToDelete: Integer; // Your ID to delete
begin
while not Terminated and (IDListIdx < IdList.Count) do
begin
IDToDelete := IDList[IDListIdx];
// ... Do whatever to delete ID
PostMessage(TheFormHandle, UM_IDDELETED, IDToDelete, 0);
end;
end;发布于 2011-03-19 19:36:31
如果您使用循环删除每个联系人,则可以调用Application.ProcessMessages,这将解决问题。
编辑调用应该在循环中
https://stackoverflow.com/questions/5364371
复制相似问题