我想让一个窗体建立在一个单独的线程中,独立余主线程,谁帮我写这个线程类.(50分)

  • 主题发起人 主题发起人 logcom
  • 开始时间 开始时间
L

logcom

Unregistered / Unconfirmed
GUEST, unregistred user!
我想让一个窗体建立在一个单独的线程中,独立余主线程,谁帮我写这个线程类.
也就是创建这个线程类实例时,就创建一个定义好的窗体.
小弟线程不熟悉,哪位大侠帮我写几句代码?
 
没有高手在吗?
 
給個範例, 要使用時就呼叫TFormTread.ExecuteThreadForm, 傳入form class即可
type
TFormThread = class(TThread)
private
FFormClass: TFormClass;
FForm: TForm;
FOldDestroy: TNotifyEvent;
protected
proceduredo
Destroy(Sender: TObject);
procedure Execute;
override;
public
class function ExecuteThreadForm(FormClass: TFormClass): TFormThread;
property Form: TForm read FForm;
end;

{ TFormThread }
procedure TFormThread.DoDestroy(Sender: TObject);
begin
if Assigned(FOldDestroy) then
FOldDestroy(Sender);
FForm := nil;
end;

procedure TFormThread.Execute;
begin
CoInitializeEx(nil, COINIT_APARTMENTTHREADED);
try
if FFormClass <> nil then
begin
FForm := FFormClass.Create(nil);
FOldDestroy := FForm.OnDestroy;
FForm.OnDestroy := Self.DoDestroy;
FForm.Show;
while (FForm <> nil) and not Terminateddo
Application.ProcessMessages;
if FForm <> nil then
FForm.Free;
end;
finally
CoUninitialize;
end;
FreeOnTerminate := True;
end;

class function TFormThread.ExecuteThreadForm(FormClass: TFormClass): TFormThread;
begin
Result := TFormThread.Create(True);
try
Result.FFormClass := FormClass;
Result.Resume;
except
Result.Free;
raise;
end;
end;
 
后退
顶部