我再一个调用线程的程序中有一个FOR循环。我发现这个FOR循环的的循环变量I从大到小赋值,可是我明明是从0到127,为什么I第一次却是128?原程序见下:
unit Unit1;interfaceuses
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.
unit Unit1;interfaceuses
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.
解决方案 »
- Delphi遇到错误可以立刻退出吗
- listview怎么设置某个单元格的颜色
- delphi中怎样使用date module
- 怎样实现按住图像来拖动窗体?
- 关于SQL Server数据库地备份和恢复问题
- 一個用BDE連接SQL Server的數據庫安裝程序,經常會因為網絡配置不正確而無法連接到數據庫,問有沒有什麼方法可以在程序中配置好網絡配置
- delphi中关于操作数据库中数据的问题
- 麻烦那位朋友帮我找一个函数!!(我已经第三次提问了)
- delphi中怎么隐藏一个进程,使其不会被ctrl+alt+del调出任务列表然后‘结束任务’?
- >>紧急求助!如何设置DBGRID单元格颜色?见者有分!<<
- 关于数据库连接时间问题!
- 在线等候!--如何用程序语句来修改DBGrid中某一个Cell中的值?
Bebin
......
end;
试一试
for i:= 0 to 127试试
a[i] := 15;lea eax, a
add eax, 4 * 127
mov ecx, 128
@loop1:
mov [eax], 15;
dec eax, 4
loop @loop1
begin
initializecriticalsection(cs);
tmythread.Create(false);
// tmythread.Create(false); // 重复了
end;
listbox1.items.Append(inttostr(globalarray[i]));
//为什么i从128开始呢????我想问题是这儿:globalarray[i]试试 加一个变量 ii : integer;然后改程序为 // if i=0 then showmessage(inttostr(i));
ii:=i;
listbox1.items.Append(inttostr(globalarray[ii]));
//为什么i从128开始呢????
procedure tform1.threaddone(sender:tobject);
var
i:integer;
begin
inc(doneflags);
entercriticalsection(cs); // 这样才能确保显示 129..256(128 项),
// 否则显示的是两个线程的混合并且是混排的结果(256项),显示结果数据的顺序取决于 CPU 时间,在此例中,线程中每赋一个值,休息 5 毫秒,因此 ListBox 中显示的第一项是线程2 产生的值(因为线程2 早等在那儿了),后面显示线程1产生的 127 个值(5毫秒足够了),再显示线程2 产生的 128 个值。
for i:=0 to maxsize do
begin
listbox1.items.Append(inttostr(globalarray[i]));
end;
leavecriticalsection(cs);
if doneflags=2 then
deletecriticalsection(cs);
end;至于你看到的 i 值为 128,这可能是 IDE 的 BUG 了,它的真实内容还是 0。
我找到了原因,只要把SLEEP(5)去掉就可以了,
但是我不明白,SLEEP这句,是在数组赋值的时候执行的吧?我跟踪调式发现,数组的值在第一个循环结束后是1128,正确的。
为什么在给LISTBOX1添加值时,开始I=128呢,数组范围是0。。127,为什么不报告边界错误呢,