主程序中用comport串口的OnRxChar()事件接收数据,并存到Tstringlist中,线程ReceiveThread是将Tstringlist中的数据取出来进行处理,现在出现的问题是系统开始运行时正常,一段时间后(时间不等)就会出现“List index out of bounds(0)”的错误,我进行try...except..进行捕捉后出现在Tstringlist.add()中,请问一下,Tstringlist.add()有特殊的地方?//主程序中串口接收字符,其中strlist为TStringlist
procedure TFrm_Main.Comm1RxChar(Sender: TObject; Count: Integer);
var str:string;
begin
comm1.ReadStr(Str,Count);
EnterCriticalSection(cs1);
try
strlist.Add(Str);
except
showmessage('添加缓存出错:');
end;
LeaveCriticalSection(cs1);
end;//线程执行代码
procedure ReceiveThread.Execute;
var str:string;
i:integer;
begin
i:=0;
while not terminated do
begin
if Frm_Main.strlist.Count>0 then
begin
try
i:=Frm_Main.strlist.Count;
str:= Frm_Main.strlist.Strings[0];
Frm_Main.strlist.Delete(0);
Frm_Main.RzStatusPane1.Caption := inttostr(i);
except
Frm_Main.RzStatusPane1.Caption := '缓存中的行数为:'+inttostr(i);
end;
Frm_Main.ProcessRecStr(str);
end
else
begin
try
i:=Frm_Main.strlist.Count;
Frm_Main.RzStatusPane1.Caption := inttostr(i);
sleep(500);
except
Frm_Main.RzStatusPane1.Caption := '缓存中的行数为0';
end;
end;
end;
end;现在系统提示的错误是“添加缓存出错”,所以应该是 strlist.Add(Str);出错。
系统中只在这两段代码中有strlist变量 。
请大家帮我分析一下是什么原因,该怎么解决
procedure TFrm_Main.Comm1RxChar(Sender: TObject; Count: Integer);
var str:string;
begin
comm1.ReadStr(Str,Count);
EnterCriticalSection(cs1);
try
strlist.Add(Str);
except
showmessage('添加缓存出错:');
end;
LeaveCriticalSection(cs1);
end;//线程执行代码
procedure ReceiveThread.Execute;
var str:string;
i:integer;
begin
i:=0;
while not terminated do
begin
if Frm_Main.strlist.Count>0 then
begin
try
i:=Frm_Main.strlist.Count;
str:= Frm_Main.strlist.Strings[0];
Frm_Main.strlist.Delete(0);
Frm_Main.RzStatusPane1.Caption := inttostr(i);
except
Frm_Main.RzStatusPane1.Caption := '缓存中的行数为:'+inttostr(i);
end;
Frm_Main.ProcessRecStr(str);
end
else
begin
try
i:=Frm_Main.strlist.Count;
Frm_Main.RzStatusPane1.Caption := inttostr(i);
sleep(500);
except
Frm_Main.RzStatusPane1.Caption := '缓存中的行数为0';
end;
end;
end;
end;现在系统提示的错误是“添加缓存出错”,所以应该是 strlist.Add(Str);出错。
系统中只在这两段代码中有strlist变量 。
请大家帮我分析一下是什么原因,该怎么解决
解决方案 »
- 一对多语音聊天室 ACM Tudpserver 只要愿意回复的,我在这里都很感激!
- dephi中怎样把多条dos语句写入记事本中,并把记事本重命名bat格式的,自动保存到c盘,并自动执行bat文件
- 菜鸟问题:dbgrid中如何设置数据的格式?谢谢各位老大!
- 兄弟帮忙看看!机子为何老有病毒!
- 如何用TStream提取和修改二进制文件里的图片,字符 搜了资料没解决!
- 虚心向大家请教几个构件企业逻辑层的基本问题,请大家指教。谢谢。
- 焦点问题.
- 用ActionManager做MDI遇到的问题。
- 关于netuseradd
- 如何把C/C++语言的*.h文件翻译成Delphi的*.pas文件,有何规则?
- Samplegrabber问题?
- DLL加载问题---急!!!11
既然能使用临界区去实现线程同步,这个低级错误应该不会有的。