unit Unit2;interfaceuses
Classes;type
Tfoothread = class(TThread)
private
{ Private declarations }
protected
procedure Execute; override;
procedure threadsdone();
end;
implementation
uses unit1,SysUtils;
{ Important: Methods and properties of objects in VCL or CLX can only be used
in a method called using Synchronize, for example, Synchronize(UpdateCaption); and UpdateCaption could look like, procedure Tfoothread.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }{ Tfoothread }
const
maxsize=20;
var
nextnumber:integer=0;
doneflags:integer=0;
cs:trtlcriticalsection; globalarray:array[1..maxsize] of integer;
function getnextnumber:integer;
begin
result:=nextnumber;
inc(nextnumber);
end;
procedure Tfoothread.Execute;
var
i:integer;
beginfor i:=1 to maxsize do
begin
globalarray[i]:=getnextnumber; synchronize(threadsdone);
end; { Place thread code here }
end;
procedure Tfoothread.threadsdone();
var
i:integer;
j:integer;
iCounter:integer; begin
InitializeCriticalSection(cs);
icounter:=0;
for i:=1 to 1000 do
begin EnterCriticalSection(cs);
j:=i;
form1.ListBox1.Items.Add(inttostr(j));
LeaveCriticalSection(cs); end;
end;end.
Classes;type
Tfoothread = class(TThread)
private
{ Private declarations }
protected
procedure Execute; override;
procedure threadsdone();
end;
implementation
uses unit1,SysUtils;
{ Important: Methods and properties of objects in VCL or CLX can only be used
in a method called using Synchronize, for example, Synchronize(UpdateCaption); and UpdateCaption could look like, procedure Tfoothread.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }{ Tfoothread }
const
maxsize=20;
var
nextnumber:integer=0;
doneflags:integer=0;
cs:trtlcriticalsection; globalarray:array[1..maxsize] of integer;
function getnextnumber:integer;
begin
result:=nextnumber;
inc(nextnumber);
end;
procedure Tfoothread.Execute;
var
i:integer;
beginfor i:=1 to maxsize do
begin
globalarray[i]:=getnextnumber; synchronize(threadsdone);
end; { Place thread code here }
end;
procedure Tfoothread.threadsdone();
var
i:integer;
j:integer;
iCounter:integer; begin
InitializeCriticalSection(cs);
icounter:=0;
for i:=1 to 1000 do
begin EnterCriticalSection(cs);
j:=i;
form1.ListBox1.Items.Add(inttostr(j));
LeaveCriticalSection(cs); end;
end;end.
Classes;type
Tfoothread = class(TThread)
private
{ Private declarations }
protected
procedure Execute; override;
procedure threadsdone();
end;
implementation
uses unit1,SysUtils;
{ Important: Methods and properties of objects in VCL or CLX can only be used
in a method called using Synchronize, for example, Synchronize(UpdateCaption); and UpdateCaption could look like, procedure Tfoothread.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }{ Tfoothread }
const
maxsize=20;
var
nextnumber:integer=0;
doneflags:integer=0;
cs:trtlcriticalsection; globalarray:array[1..maxsize] of integer;
function getnextnumber:integer;
begin
result:=nextnumber;
inc(nextnumber);
end;
procedure Tfoothread.Execute;
var
i:integer;
beginfor i:=1 to maxsize do
begin
globalarray[i]:=getnextnumber; synchronize(threadsdone);
end; { Place thread code here }
end;
procedure Tfoothread.threadsdone();
var
i:integer;
j:integer;
iCounter:integer; begin
InitializeCriticalSection(cs);
icounter:=0;
for i:=1 to 1000 do
begin EnterCriticalSection(cs);
j:=i;
form1.ListBox1.Items.Add(inttostr(j));
LeaveCriticalSection(cs); end;
end;end.
那你要在进循环之前就进入临界区
在出循环之后出临界区
前声明试试
InitializeCriticalSection(Critical1)
//在窗体销毁中
DeleteCriticalSection(Critical1)
//在线程中
EnterCriticalSection(Critical1)
.........保护代码
LeaveCriticalSection(Critical1)