设计一个简单的多线程(Fecit)_1
D6高级编程,Fecit ,学习里面关于线程创建的一个例子.,按照那个例子做的,
不过本人喜欢将线程实现部分作为单独的单元,主线程再调用它。
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; Button2: TButton; Button3: TButton; Button4: TButton; Edit1: TEdit; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); procedure Button4Click(Sender: TObject); private { Private declarations } public { Public declarations } end; TMyThread = class(TThread) Count:integer; MyEdit:Tedit; procedure Show;virtual;abstract; constructor Create(myedit1:tedit); end; Thread1 = class(TMyThread) procedure Show;override; procedure Execute;override; end; Thread2 = class(TMyThread) procedure Show;override; procedure Execute;override; end; var Form1: TForm1; implementation procedure MyThreadFunc; var i:integer; dc:hdc; s:string; begin for i:=0 to 1000000 do begin s:=inttostr(i); dc:=getdc(Form1.Edit1.Handle); TextOut(dc,0,0,pchar(s),length(s)); ReleaseDC(form1.Edit1.handle,dc); end; end; constructor TMyThread.Create(MyEdit1:Tedit); begin inherited Create(false); MyEdit:=MyEdit1; FreeOnTerminate:=True; end; procedure Thread1.show; begin MyEdit.Text:=inttostr(Count); end; procedure Thread1.execute; var i:integer; begin for i:=0 to 1000000 do begin count:=i; Synchronize(Show); end; end; procedure Thread2.show; begin MyThreadFunc; end; procedure Thread2.Execute; begin Synchronize(Show); end; {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); begin with Thread1.create(Edit1) do end; procedure TForm1.Button2Click(Sender: TObject); begin with Thread2.create(Edit1) do end; procedure TForm1.Button3Click(Sender: TObject); var hthread:Thandle; thid:dword; begin hthread:=Beginthread(nil,0,@MyThreadFunc,nil,0,thid); if hthread=0 then showmessage('failed'); end; procedure TForm1.Button4Click(Sender: TObject); begin MyThreadFunc; end; end. end.
OnionYang@