最近遇到一个非常棘手的问题,复现频率及其的低.
形同运行环境描述:
只在客户端复现过(客户端XP或者2003),我本地是win7问题描述:
在程序的主界面放了一个定时器控件,定时器Timer事件的逻辑如下,
tmrWork.Enabled := False;
tmrWork.Interval := 30 * 1000; //不在运行时间,半分钟检测一次
TSecThread.Create(StrList);TSecThread类如下
TSecThread = class(TThread)
private
strList:TStrings;
ConText : TConText;
public
procedure Execute; override;
procedure SetstrList(var strL:TStrings);
constructor Create(var strL:TStrings);
end;
constructor TSecThread.Create(var strL: TStrings);
begin
SetstrList(strL);
StataCon := TConText.Create(strList) ;
FreeOnTerminate := True;
inherited Create(False);
end;在线程TSecThread.Execute中处理逻辑如下,try finally部分做了业务处理,然后在finally end部分做了定时器的处理
inherited;
try
ConText.NowState.SetNextState();
Sleep(100);
ConText.NowState.SetNextState();
Sleep(100);
ConText.NowState.SetNextState();
finally
if Assigned(ConText) then
freeandnil(ConText);
FmMain.tmrWork.Enabled := True;
end;问题出现了,安装上面的逻辑,只能有一个TSecThread线程在执行,可是有的时候会出现两个TSecThread在执行,请问有人出现过这样的问题没有?怎么解决?
形同运行环境描述:
只在客户端复现过(客户端XP或者2003),我本地是win7问题描述:
在程序的主界面放了一个定时器控件,定时器Timer事件的逻辑如下,
tmrWork.Enabled := False;
tmrWork.Interval := 30 * 1000; //不在运行时间,半分钟检测一次
TSecThread.Create(StrList);TSecThread类如下
TSecThread = class(TThread)
private
strList:TStrings;
ConText : TConText;
public
procedure Execute; override;
procedure SetstrList(var strL:TStrings);
constructor Create(var strL:TStrings);
end;
constructor TSecThread.Create(var strL: TStrings);
begin
SetstrList(strL);
StataCon := TConText.Create(strList) ;
FreeOnTerminate := True;
inherited Create(False);
end;在线程TSecThread.Execute中处理逻辑如下,try finally部分做了业务处理,然后在finally end部分做了定时器的处理
inherited;
try
ConText.NowState.SetNextState();
Sleep(100);
ConText.NowState.SetNextState();
Sleep(100);
ConText.NowState.SetNextState();
finally
if Assigned(ConText) then
freeandnil(ConText);
FmMain.tmrWork.Enabled := True;
end;问题出现了,安装上面的逻辑,只能有一个TSecThread线程在执行,可是有的时候会出现两个TSecThread在执行,请问有人出现过这样的问题没有?怎么解决?
解决方案 »
- 初学。怎么把EXCEL转换成文本.txt的?100
- 我用的是sqlserver的数据库,怎么样在数据库里面存取图片呢?
- 怎么让一个string里面能包含'这样的字符呢
- 100分求线程,多线程方面的例子,高手狂进啊!!
- 不解,前途迷茫
- *****还散分*****郁闷中。。。。。。。。。。
- D7+MSsqlserver2000程序,通过ADOQuery对表进行付值时出现的问题。我用以下语句为数据表列赋值,数据表列[decimal]型(28位,10位小数)。ADO
- 怎么总是报错啊?
- 用JetEngine压缩Access数据库的问题
- 不知是个好主意还是馊主意,关于DLL、COM组件的引用计数问题。详情......
- 村子里的新与旧
- delphi怎么向一个文本文件的每行添加某个字符串??????????????????????????????????
procedure TSecThread.Execute;
var
MsgRec: TMsg;
begin
inherited;
FCheckTimer := TTimer.Create(nil);
FCheckTimer.Interval := 30*1000;
FCheckTimer.OnTimer := procTimer;
FCheckTimer.Enabled := true; while GetMessage(MsgRec, 0, 0, 0) do
begin
TranslateMessage(MsgRec);
DispatchMessage(MsgRec);
end;
FCheckTimer.Enabled := False;
FCheckTimer.free;
end;procedure TSecThread.procTimer(Sender: TObject);
begin
FCheckTimer.Enabled := False;
try
//do something
finally
FCheckTimer.Enabled := true;
end;
end;
定时器是主线程运行。
没明白是什么意思.
// 这里是你的状态逻辑语句
Sleep(30000); // 等待半分钟重新开始
until Terminated;这样就能不断循环处理,直到你把 Terminated 设成 True。为了能在终止时尽快反应,可以把 Sleep 缩短,用循环凑成半分钟,中间不断检测 Terminated 条件。
就是timerTo: jinghai1776
这个办法比较好,不过看看还有其他的办法没有,改动有些大,这个是最后的选择了。
定时器事件的代码没贴,不知道里面怎么写,有没有先停止自己再启动线程?如果的确是每次执行完停止30秒再重新执行,的确可以在线程里sleep
try
ConText.NowState.SetNextState();
Sleep(100);
ConText.NowState.SetNextState();
Sleep(100);
ConText.NowState.SetNextState();
finally
if Assigned(ConText) then
freeandnil(ConText);
FmMain.tmrWork.Enabled := True;
end;---------------------------------------------------------FmMain.tmrWork.Enabled := True;
这句有问题。TTimer是独立于线程的对象。你这句应该用Synchronous执行。