线程代码如下:
procedure TAlarmThread.Execute;
var
tmpr:pAlarmListItem;
begin
while not terminated do
begin
if AlarmLists.AlarmNow.WaitFor(infinite)=wrSignaled then
begin
AlarmLists.AlarmNow.ResetEvent;
AlarmLists.lock.Enter;
tmpr := AlarmLists.tail;
while tmpr<>AlarmLists.head do
begin
AlarmLists.lock.Leave;
RA:=tmpr^.data;
if abs(RA.Alevel)<=3 then begin
if RA.CHID = $ff then
Synchronize(DealWithCommErr)
else begin
NewAlarm := True;
NewAlarm1 := True;
Synchronize(AlarmAna);
ASStr := DMForm.GetAlarmStyle(RA.GateWayID,RA.CCUID,RA.DPUID,RA.CHID);
LogicAdr := DMForm.GetLogicAdr2(RA.GateWayID,RA.CCUID,RA.DPUID,RA.CHID); if LogicAdr <>'--' then
begin
if MainForm.AlarmEnable(ASStr,abs(RA.Alevel)) then
if NewAlarm or NewAlarm1 then Synchronize(AddToList);
end;
end;
end;
with AlarmLists do
begin
lock.Enter;
tmpr^.prev^.next := tmpr^.next;
if tmpr=tail then
tail := tmpr^.prev
else
tmpr^.next^.prev := tmpr^.prev;
dispose(tmpr);
dec(NodesNum);
lock.Leave;
end;
AlarmLists.lock.Enter;
tmpr:=tmpr^.prev;
end;
AlarmLists.lock.Leave;
end;
end;
end;其中的RA是一个全局的结构变量,AlarmAna和AddToList是两个线程方法,我想问这个线程有问题吗?
procedure TAlarmThread.Execute;
var
tmpr:pAlarmListItem;
begin
while not terminated do
begin
if AlarmLists.AlarmNow.WaitFor(infinite)=wrSignaled then
begin
AlarmLists.AlarmNow.ResetEvent;
AlarmLists.lock.Enter;
tmpr := AlarmLists.tail;
while tmpr<>AlarmLists.head do
begin
AlarmLists.lock.Leave;
RA:=tmpr^.data;
if abs(RA.Alevel)<=3 then begin
if RA.CHID = $ff then
Synchronize(DealWithCommErr)
else begin
NewAlarm := True;
NewAlarm1 := True;
Synchronize(AlarmAna);
ASStr := DMForm.GetAlarmStyle(RA.GateWayID,RA.CCUID,RA.DPUID,RA.CHID);
LogicAdr := DMForm.GetLogicAdr2(RA.GateWayID,RA.CCUID,RA.DPUID,RA.CHID); if LogicAdr <>'--' then
begin
if MainForm.AlarmEnable(ASStr,abs(RA.Alevel)) then
if NewAlarm or NewAlarm1 then Synchronize(AddToList);
end;
end;
end;
with AlarmLists do
begin
lock.Enter;
tmpr^.prev^.next := tmpr^.next;
if tmpr=tail then
tail := tmpr^.prev
else
tmpr^.next^.prev := tmpr^.prev;
dispose(tmpr);
dec(NodesNum);
lock.Leave;
end;
AlarmLists.lock.Enter;
tmpr:=tmpr^.prev;
end;
AlarmLists.lock.Leave;
end;
end;
end;其中的RA是一个全局的结构变量,AlarmAna和AddToList是两个线程方法,我想问这个线程有问题吗?
EnterCriticalSection(cs);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//离开临界
LeaveCriticalSection(cs);用信号量控制
CreateCriticalSection(cs);注意,全局变量都必须使用阻塞或者互技术
RA:=tmpr^.data;
if abs(RA.Alevel)<=3 then begin
if RA.CHID = $ff then
Synchronize(DealWithCommErr)
else begin
NewAlarm := True;
NewAlarm1 := True;
Synchronize(AlarmAna);
ASStr := DMForm.GetAlarmStyle(RA.GateWayID,RA.CCUID,RA.DPUID,RA.CHID);
LogicAdr := DMForm.GetLogicAdr2(RA.GateWayID,RA.CCUID,RA.DPUID,RA.CHID); if LogicAdr <>'--' then
begin
if MainForm.AlarmEnable(ASStr,abs(RA.Alevel)) then
if NewAlarm or NewAlarm1 then Synchronize(AddToList);
end;
end;
end;
赋值之后使用Leav……
AlarmLists.lock.Enter的线程阻塞,一次出错,线程永远都进不去了,而这种错误很难调试,建议有良好的编程风格.其他的没有仔细看,太长了,而且很多变量不知道啥玩意.