考虑以下类:自定义线行为不端
type
GEvent = class(TThread)
public
procedure Terminate;
procedure Call(Event : GEvent);
constructor Create;
procedure Execute; Override;
end;
TDirection = (DUp, DRight, DDown, DLeft);
EventTitle = class(GEvent)
private
Index : Integer;
Sprite : CSprite;
Terminate : Boolean;
procedure CreateSprite;
procedure MoveCursor(Direction : TDirection);
procedure RefreshCursor;
constructor Create;
destructor Destroy;
public
procedure Execute;
end;
implementation
{ GEvent }
procedure GEvent.Call(Event: GEvent);
begin
Suspend;
// inherited Terminate;
Self := GEvent(Event.ClassType.Create);
end;
constructor GEvent.Create;
begin
inherited Create(True);
end;
destructor GEvent.Destroy;
begin
Terminate;
inherited;
end;
procedure GEvent.Execute;
begin
// inherited;
end;
procedure GEvent.Terminate;
begin
Suspend;
inherited;
end;
{ EventTitle }
constructor EventTitle.Create;
begin
inherited;
Resume;
end;
procedure EventTitle.CreateSprite;
begin
Showmessage('anything');
end;
destructor EventTitle.Destroy;
begin
inherited;
end;
procedure EventTitle.Execute;
begin
inherited;
Synchronize(CreateSprite);
Index := 0; {
while not Terminated do
begin
if GISystem.System.Input.Trigger(KUp) then
MoveCursor(DUp);
if GISystem.System.Input.Trigger(KDown) then
MoveCursor(DDown);
end; }
end;
当主窗体调用InstanceVar := EventTitle.Create
自动线程应达到的方法CreateSprite
,什么古怪的是没有发生。我不明白为什么该方法没有被执行。该应用程序的主要形式仍然正常工作,但它似乎像EventTitle.Execute
突然停止,甚至不启动。它可能也是错误实现。这是我的第一个多线程试用版,然后对任何不一致性抱歉。任何人都可以看到我做错了什么?
OT:不要使用暂停和恢复。当编译器在警告中报告你时,它们已被废弃......同时创建线程作为立即恢复的暂停状态看起来并不太有用。 – TLama
真的。我会让'Resume'给子线程。删除它...... – Guill
也许使用OmniThreadLibrary对你来说比对TThread内部进行挖掘更容易 –