考慮以下類:自定義線行爲不端
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內部進行挖掘更容易 –