Can be running environment description:
Only on the client side retrieval (client XP or 2003), I am local Windows 7
Problem description:
The main interface of the program put a Timer control, Timer Timer event logic is as follows,
TmrWork. Enabled:=False;
TmrWork. Interval: 30 *=1000;//isn't running time, half a minute to detect a
TSecThread. Create (StrList);
TSecThread class the following
TSecThread=class (TThread)
Private
StrList: TStrings;
ConText: TConText;
Public
Procedure the Execute; Override.
Procedure SetstrList (var strL: TStrings);
The constructor Create (var strL: TStrings);
end;
Constructor TSecThread. Create (var strL: TStrings);
The begin
SetstrList (strL);
StataCon:=TConText. Create (strList);
FreeOnTerminate:=True;
Inherited the Create (False);
end;
In a thread TSecThread processing logic in the Execute as follows, try finally part did business processing, and then in the finally end part timer processing
Inherited;
Try
The ConText. NowState. SetNextState ();
Sleep (100);
The ConText. NowState. SetNextState ();
Sleep (100);
The ConText. NowState. SetNextState ();
The finally
If Assigned (ConText) then
Freeandnil (ConText);
FmMain. TmrWork. Enabled:=True;
end;
problem, install the above logic, there can be only one TSecThread threads in the execution, but sometimes there will be two TSecThread in execution, could you tell me some of this problem? How to solve?
CodePudding user response:
Didn't understand what you are talking aboutThe timer can be created in the thread
CodePudding user response:
Procedure TSecThread. Execute;
Var
MsgRec: TMsg;
The begin
Inherited;
FCheckTimer:=TTimer. Create (nil);
FCheckTimer. Interval: 30 *=1000;
FCheckTimer. OnTimer:=procTimer;
FCheckTimer. Enabled:=true;
While GetMessage (MsgRec, 0, 0, 0) do
The begin
The TranslateMessage (MsgRec);
DispatchMessage (MsgRec);
end;
FCheckTimer. Enabled:=False;
FCheckTimer. Free;
end;
Procedure TSecThread. ProcTimer (Sender: TObject);
The begin
FCheckTimer. Enabled:=False;
Try
//do something
The finally
FCheckTimer. Enabled:=true;
end;
end;
CodePudding user response:
I want through the timer to control the thread,CodePudding user response:
Timer is not very accurate, such as in TSecThread. You Create (StrList); Judgment is done first,The timer is the main thread to run,
CodePudding user response:
To: andrew57Didn't understand what that means.
CodePudding user response:
Time precision is not important, my directory is through the start timer control thread, timer trigger, give the child thread processing task, after processing, the timer will be activated, such guarantee my threads run infinite loop, and only one thread in work,but the problem is, sometimes he will appear two child thread colleagues in processing tasks,
CodePudding user response:
How to write the timer event?In addition, the thread started to execute, as if also didn't close the timer action? Only the last open timer code
CodePudding user response:
, it is not necessary to use timer, it does not have to be repeated to create and release the thread, write directly in the Execute a loop:Repeat
//here's your state logic statements
Sleep (30000);//wait for half a minute to start
Until Terminated;
Cycle so that you can handle until you establish Terminated the True, in order to carry on as soon as possible at the termination of the reaction, can shorten the Sleep, use cycle add up to half a minute, constantly testing among Terminated conditions,
CodePudding user response:
To: sz_haitaoThe timer is
To: jinghai1776
This way is better, but see there are other way, without some major changes, this is the last choice,
CodePudding user response:
How to write the timer event?Timer event code didn't stick, don't know how to write, stop yourself first, then starting a thread?
If every time is 30 seconds after stopping execution again, really can sleep in the thread
CodePudding user response:
You might as well think about carefully: in the main thread thread start timer, and create a new thread in the timer event, it's likely that two threads at the same time, because the old threads it takes time to release, and this kind of phenomenon is random, because it depends on the operating system how to allocate the CPU time slice, this scheme even don't want to think,CodePudding user response: