Mega Code Archive

 
Categories / Delphi / Examples
 

Create Threads without using TThreadClass

Title: Create Threads without using TThreadClass Question: How to create threads without using the TThread Class in Delphi? Answer: You can create threads withoutusingthe TThread Class bymaking use of the WinAPI functions CreateThread, SuspendThread, Resume Thread and Terminate Thread functions Create 2 edit controls and 6 buttons The code is given below unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; procedure incedit1;stdcall; procedure incedit2;stdcall; type TForm1 = class(TForm) Button1: TButton; Edit1: TEdit; Edit2: TEdit; Button6: TButton; Button7: TButton; Button2: TButton; Button3: TButton; Button5: TButton; Button4: TButton; procedure Button1Click(Sender: TObject); procedure Button6Click(Sender: TObject); procedure Button7Click(Sender: TObject); procedure Button4Click(Sender: TObject); procedure Button5Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation var th1,th2:cardinal; h1,h2:integer; procedure incedit1; var i:integer; begin i:=0; while true do begin form1.edit1.text:=inttostr(i); i:=i+1; end; end; procedure incedit2; var i:integer; begin i:=0; while true do begin form1.edit2.text:=inttostr(i); i:=i+1; end; end; {$R *.DFM} procedure TForm1.Button1Click(Sender: TObject); begin h1:=beginthread(nil,1024,@incedit1,nil,0,th1); h2:=beginthread(nil,1024,@incedit2,nil,0,th2); end; procedure TForm1.Button6Click(Sender: TObject); begin terminatethread(h1,0); end; procedure TForm1.Button7Click(Sender: TObject); begin terminatethread(h2,0); end; procedure TForm1.Button4Click(Sender: TObject); begin resumethread(h1); end; procedure TForm1.Button5Click(Sender: TObject); begin resumethread(h2); end; procedure TForm1.Button2Click(Sender: TObject); begin suspendthread(h1); end; procedure TForm1.Button3Click(Sender: TObject); begin suspendthread(h2); end;