Home > Back-end >  Delphi timer and between threads
Delphi timer and between threads

Time:10-21

Recently encountered a very thorny problem, repetition frequency and the low.
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 about
The 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: andrew57
Didn'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_haitao
The 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:

11 references jinghai1776 response:
just think carefully: you start the main thread in the thread 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,


Oh, the original has been posted timer event code, is to stop their first restart thread
30 seconds after the thread to start the timer, timer stop yourself again after restarting threads
In theory, the interval of 30 seconds is how all old thread to release enough
May not be released, just pause and restart may be good

, of course, this case directly in the thread suspended for 30 seconds more reasonable, simple and reliable

CodePudding user response:

In the timer by WaitForSingleObject (handle, 0); Give it a try

CodePudding user response:

1. Write a infinite loop inside thread, each delay half a minute not same effect?
2. You can define a global variable identifies the current career in running,

CodePudding user response:

Didn't see how this

CodePudding user response:

Repeatedly create a thread that is not recommended if TSecThread. Create must be outside your timer event you have done, I think TSecThread had two reasons is not the timer, but outside the timer TSecThread. Create was carried out twice,

CodePudding user response:

Try to restore the timer code in Thread OnTerminate event

CodePudding user response:

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;

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

FmMain. TmrWork. Enabled:=True;
It has a problem, TTimer is independent of the thread object, you this sentence should use Synchronous implementation,

CodePudding user response:

Timer is in itself a thread.nullnullnullnullnullnullnullnullnullnullnull
  • Related