Delphi 动态创建不同线程类(继承自一个线程基类)
作者:互联网
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type tmythreadclass = class of tmybasethread; //基类 tmybasethread= class(TThread) private procedure test; protected procedure Execute; override; public constructor Create(CreateSuspended: Boolean); virtual; //要设置成虚函数 不设置的话,继承的类就不会执行到create 来 end; //继承派生mythread1 Tmythread1 =class(tmybasethread) private procedure test; protected procedure Execute; override; public constructor Create(CreateSuspended: Boolean);override; end; TForm1 = class(TForm) btn1: TButton; mmo1: TMemo; procedure btn1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} constructor tmybasethread.Create(CreateSuspended: Boolean); begin inherited Create(False); // Form1.mmo1.Lines.Add('tmybasethread'); end; procedure tmybasethread.Execute; begin FreeOnTerminate := True; Synchronize(test); end; procedure tmybasethread.test; begin form1.mmo1.Lines.Add('tmybasethread'); end; constructor Tmythread1.Create(CreateSuspended: Boolean); begin inherited Create(False); end; procedure Tmythread1.Execute; begin inherited; Synchronize(test); end; procedure Tmythread1.test; begin form1.mmo1.Lines.Add('Tmythread1'); end; procedure TForm1.btn1Click(Sender: TObject);
//模仿applicaiton.createform的写法 procedure threadcreate(athreadclass:tmythreadclass; var mythread); var Instance: tmybasethread; begin Instance := tmybasethread(athreadclass.NewInstance); tmybasethread(mythread) := Instance; try Instance.Create(False); except TComponent(mythread) := nil; raise; end; end; var my1:Tmythread1; begin threadcreate(Tmythread1, my1); my1.Suspend; end; end.
标签:Tmythread1,begin,tmybasethread,Delphi,动态创建,线程,end,Create,procedure 来源: https://www.cnblogs.com/BTag/p/16370835.html