我用类似死循环的写法即
while Not Terminated do
begin
///code
end;不行因为这样CPU老是100%,而且我也不需要他一直执行,只需要他隔断时间执行一次就可以了,为此,我在主线程中用了一个线程Timer控件来调度它们,(20多个子线程),线程执行体改为执行一次后Suspend,然后在线程TIMER中去resume,但是这样不知道为什么不行?好象执行一次后,那些线程就一直挂起了,不能在Resume了,还有我的线程执行体里面有很多操作VCL的代码?有影响吗??请问各位你们是怎么处理这种问题!本人先再次谢谢了!
while Not Terminated do
begin
///code
end;不行因为这样CPU老是100%,而且我也不需要他一直执行,只需要他隔断时间执行一次就可以了,为此,我在主线程中用了一个线程Timer控件来调度它们,(20多个子线程),线程执行体改为执行一次后Suspend,然后在线程TIMER中去resume,但是这样不知道为什么不行?好象执行一次后,那些线程就一直挂起了,不能在Resume了,还有我的线程执行体里面有很多操作VCL的代码?有影响吗??请问各位你们是怎么处理这种问题!本人先再次谢谢了!
while Not Terminated do
begin
DaoQi :=false;
SendDbgMsg(ThreadNo,'线程采集信息',2,0,0,' ','高'); //发送开始采集消息
SendDbgMsg(ThreadNo,'线程采集信息',2,0,0,'线程开始采集','高'); //发送开始采集消息
SendDbgMsg(ThreadNo,'线程采集信息',2,0,0,'开始遍历'+inttostr(ThreadNo)+'号线程下的通道','高'); //遍历通道 for chanelloop :=0 to length(chans)-1 do //开始该线程下的通道循环
begin
for dcploop :=0 to length(chans[chanelloop].dcps)-1 do
begin
dcptemp :=chans[chanelloop].dcps[dcploop].dcps;
for crsloop := 0 to length(dcptemp.crsno)-1 do //开始调度方案的遍历
begin
if ((dcptemp.dcpstatus=1) or (dcptemp.dcpstatus=2)) and (MakeDateTime(dcptemp.crsbase[crsloop],dcptemp.crscircle[crsloop],dcptemp.crsunit[crsloop])<=Now) and (dcptemp.crsbase[crsloop]<>0) then //如果该采集点在用,或部分在用且调度方案到期
begin
DaoQi :=true;
break;
end;
end;
if DaoQi then break;
end; if DaoQi then
begin
SendDbgMsg(ThreadNo,'线程采集信息',2,0,0,'有采集点到期,开始采集.........................','高'); //到期
// 根据本通道类型,创建局部变量channel通道类实例 //创建的具体参数,等设计好通道类后,来填充,其参数来自通道结构体
case chans[chanelloop].chans_type of
1: channel :=(TSeriesPort.Create) as TChannel;
2: channel :=(TNetRouter.Create('127.0.0.1',1244)) as TChannel;
3: channel :=(TDial.Create) as TChannel;
4: channel :=(TNRDial.Create) as TChannel;
end;
ifopen :=true;
try
channel.open(); //打开成功还是失败??????????? 代开端口
except
SendDbgMsg(ThreadNo,'线程采集信息',2,0,0,'通道打开端口时失败,请检查该端口是否被占用','高');
ifopen :=false;
end;
if ifopen then
begin
SendDbgMsg(ThreadNo,'线程采集信息',2,0,0,'通道建立成功','高');
SendDbgMsg(ThreadNo,'线程采集信息',2,0,0,'通道打开端口成功','高');
SendDbgMsg(ThreadNo,'线程采集信息',2,0,0,'开始本通道下的采集点循环','高');
for dcploop :=0 to length(chans[chanelloop].dcps)-1 do //开始本通道下的采集点循环
begin
dcptemp :=chans[chanelloop].dcps[dcploop].dcps;
DcpNo :=dcploop;
DcpState :=4;
Synchronize(SetDcpState); // Synchronize(SetDcpState); //置开始采集状态
SendDbgMsg(ThreadNo,'线程采集信息',2,DcpNo,0,'正在创建规约类.................................','高');
case dcptemp.dcpprotocol of
1: protocol :=Tsiemens2510.Create as TProtocol;
2: protocol :=Tweiyuan102.Create as TProtocol; //规约类的创建参数???
else ; //其他规约类
end; SendDbgMsg(ThreadNo,'线程采集信息',2,DcpNo,0,'开始本采集点下的调度方案循环....................','高');
for crsloop := 0 to length(dcptemp.crsno)-1 do //开始本采集点下的调度方案
begin
if (((dcptemp.dcpstatus=1) or (dcptemp.dcpstatus=2)) and
((MakeDateTime(dcptemp.crsbase[crsloop],dcptemp.crscircle[crsloop],dcptemp.crsunit[crsloop])<=Now)) and (dcptemp.crsbase[crsloop]<>0)) or
((dcptemp.laststatus[dcptemp.crsno[crsloop]]=0) or (dcptemp.laststatus[dcptemp.crsno[crsloop]]=1)) then
begin
/// 将基准时间一个周期一个周期地向后调整
starttime :=dcptemp.crsbase[crsloop];
while starttime<=now do
begin
chans[chanelloop].dcps[dcploop].dcps.crsbase[crsloop]:=starttime;
starttime :=makedatetime(starttime,dcptemp.crscircle[crsloop],dcptemp.crsunit[crsloop])
end
end;
////根据调度方案中的调度编码开始采集相应的信息
case dcptemp.crsno[crsloop] of
MASK_NO1:
begin
SendDbgMsg(ThreadNo,'线程采集信息',2,DcpNo,0,'抄电量1被调度...............','高');
GetDianLiang1(); //抄电量1
end;
MASK_NO2: //抄瞬时量数据
begin
SendDbgMsg(ThreadNo,'线程采集信息',2,DcpNo,0,'抄瞬时量被调度...............','高');
GetSSL();
end;
MASK_NO13:
begin
SendDbgMsg(ThreadNo,'线程采集信息',2,DcpNo,0,'抄电量2被调度...............','高');
GetDianLiang2(); //抄电量2
end;
MASK_NO14:
begin
SendDbgMsg(ThreadNo,'线程采集信息',2,DcpNo,0,'抄电量3被调度...............','高');
GetDianLiang3(); //抄电量3
end;
MASK_NO15:
begin
SendDbgMsg(ThreadNo,'线程采集信息',2,DcpNo,0,'抄电量4被调度...............','高');
GetDianLiang4(); //抄电量4
end;
MASK_NO16: //系统对时
begin
SendDbgMsg(ThreadNo,'线程采集信息',2,DcpNo,0,'系统对时被调度...............','高');
SysCompTime(); //系统对时
end;
end; //end case
end; //end crs
if Assigned(protocol) then
protocol.Destroy;
end; //end for
channel.close(); //关闭端口
channel.Destroy; //释放销毁该通道
end;// end if ifopen
end else //end if daoqi
begin
SendDbgMsg(ThreadNo,'线程采集信息',2,0,0,'无采集点到期,本次执行结束','高'); //无到期
end;
end; //end for chanel
if Assigned(mpsdl) then
begin
setLength(mpsdl,0);
mpsdl :=nil;
end;
delay(5000);
Suspend;
end; //end while
begin
//your code;
//设置一个时间间隔
end;设置时间间隔可以用gettickcount
begin
///code
sleep(1000);
end;
begin
///code
Application.ProcessMessages ;
end;
while Not Terminated do
begin
.....
//下面开始进入等待状态,在你需要继续运行线程时,执行 AEvent.SetEvent;
// AEvent 是 TEvent 类型
// 也可以通过信号量方式处理。
while AEvent.WaitFor(nnnnnn)<>wrSignaled do;
end;procedure Tform1.Timer1Timer(Sender:TObject);
begin
AEvent.SetEvent;
end;
那什么时候执行AEvent.ResSetEvent呢????
2,cpu的问题。一般情况下,你需要线程进行sleep一段时间。比如sleep(50)。不过一般在非ring0级程序的使用,一般是50毫秒的单位。这样,你的线程就不会过分的占用cpu。
3,如果你的程序需要等待某个信号,建议使用waitforobject()函数。
在 Waitfor 时,也会 ResetEvent