有的就是分数,想问一个简单的问题!希望有熟悉的高手解决。分数不够可以追加!在线等待!!!
我再一个调用线程的程序中有一个FOR循环。我发现这个FOR循环的的循环变量I从大到小赋值,可是我明明是从0到127,为什么I第一次却是128?原程序见下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
ListBox1: TListBox;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
procedure threaddone(sender:tobject);
public
{ Public declarations }
end;
tmythread=class(tthread)
protected
procedure execute;override;
end;
var
Form1: TForm1;
implementation
const
maxsize=127;
var
doneflags: integer=0;
globalarray:array[0..127] of integer;
nextnumber:integer=0;
cs:trtlcriticalsection;
{$R *.dfm}
function getnextnumber:integer;
begin
inc(nextnumber);
result:=nextnumber;
end;
procedure tmythread.execute;
var
i:integer;
begin
onterminate:=form1.threaddone;
entercriticalsection(cs);
//
for i:=0 to maxsize do
begin
globalarray[i]:=getnextnumber; //1..128
sleep(5);
end;
//
leavecriticalsection(cs);
end;
procedure tform1.threaddone(sender:tobject);
var
i:integer;
begin
inc(doneflags);
for i:=0 to maxsize do
begin
// if i=0 then showmessage(inttostr(i));
listbox1.items.Append(inttostr(globalarray[i]));
//为什么i从128开始呢????
end;
if doneflags=2 then
deletecriticalsection(cs);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
initializecriticalsection(cs);
tmythread.Create(false);
tmythread.Create(false);
end;
end.