T
true_feiyun
Unregistered / Unconfirmed
GUEST, unregistred user!
unit Unit831;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
ListBox1: TListBox;
Button2: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
procedure add;
procedure dec;
public
{ Public declarations }
end;
var
Form1: TForm1;
count,num:integer;
good:boolean;
sect:trtlcriticalsection;
implementation
{$R *.dfm}
{ TForm1 }
procedure TForm1.add;
var
i,j:integer;
s:string;
begin
for j:=1 to countdo
begin
if good then
entercriticalsection(sect);
num:=num+10;
i:=num-10;
sleep(5);
s:=format('the value of number is:%d',);
sendmessage(form1.ListBox1.Handle,lb_addstring,0,longint(s));
num:=num-10;
if good then
leavecriticalsection(sect);
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
num:=100;
count:=30;
initializecriticalsection(sect);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
threadid1,threadid2:dword;
begin
good:=false;
listbox1.Clear;
createthread(nil,0,@tform1.add,nil,0,threadid1);
createthread(nil,0,@tform1.dec,nil,0,threadid2);
end;
procedure TForm1.dec;
var
i,j:integer;
s:string;
begin
for j:=1 to countdo
begin
if good then
entercriticalsection(sect);
num:=num-10;
i:=num+10;
sleep(20);
s:=format('the value of number is:%d',);
sendmessage(form1.ListBox1.Handle,lb_addstring,0,longint(s));
num:=num+10;
if good then
leavecriticalsection(sect);
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
threadid1,threadid2:dword;
begin
good:=true;
listbox1.Clear;
createthread(nil,0,@tform1.add,nil,0,threadid1);
createthread(nil,0,@tform1.dec,nil,0,threadid2);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
deletecriticalsection(sect);
end;
end.
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
ListBox1: TListBox;
Button2: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
procedure add;
procedure dec;
public
{ Public declarations }
end;
var
Form1: TForm1;
count,num:integer;
good:boolean;
sect:trtlcriticalsection;
implementation
{$R *.dfm}
{ TForm1 }
procedure TForm1.add;
var
i,j:integer;
s:string;
begin
for j:=1 to countdo
begin
if good then
entercriticalsection(sect);
num:=num+10;
i:=num-10;
sleep(5);
s:=format('the value of number is:%d',);
sendmessage(form1.ListBox1.Handle,lb_addstring,0,longint(s));
num:=num-10;
if good then
leavecriticalsection(sect);
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
num:=100;
count:=30;
initializecriticalsection(sect);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
threadid1,threadid2:dword;
begin
good:=false;
listbox1.Clear;
createthread(nil,0,@tform1.add,nil,0,threadid1);
createthread(nil,0,@tform1.dec,nil,0,threadid2);
end;
procedure TForm1.dec;
var
i,j:integer;
s:string;
begin
for j:=1 to countdo
begin
if good then
entercriticalsection(sect);
num:=num-10;
i:=num+10;
sleep(20);
s:=format('the value of number is:%d',);
sendmessage(form1.ListBox1.Handle,lb_addstring,0,longint(s));
num:=num+10;
if good then
leavecriticalsection(sect);
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
threadid1,threadid2:dword;
begin
good:=true;
listbox1.Clear;
createthread(nil,0,@tform1.add,nil,0,threadid1);
createthread(nil,0,@tform1.dec,nil,0,threadid2);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
deletecriticalsection(sect);
end;
end.