[Lazarus] timer in a console application

Andrea Mauri andrea.mauri.75 at gmail.com
Fri Aug 10 15:08:30 CEST 2012


Il 10/08/2012 14:59, Sven Barth ha scritto:
>>>   while not fStopExecution do
>>>     begin
>>>     CheckSynchronize;
>>>     inc(i);
>>>     end;
>> This results in 100 % CPU (in a single CPU system) so not a very good
>> idea IMHO. (Even the timer thread is crippled by the main thread.)
>>
>> If you can live with some latency I would do something like:
>>
>>     while not fStopExecution do
>>       begin
>>       CheckSynchronize;
>>       sleep(Max_Latancy);
>>       end;
>
> You know that the "inc(i)" in the above code is where the author's
> calculation takes place? So the "CheckSynchronize" call would just be
> between different calculation steps where it's safe to have the
> execution stopped (though it might be better to put the
> "CheckSynchronize" to the end of the loop).


Using CheckSynchronize the OnTimer event is fired.

I will try to better explain my problem.

My app must be as fast as possible, is checksynchronize cpu demanding?
Here a simplified representation of my app:

begin
   loadMoleculeFiles;
   for i:= 0 to numberofmolecules-1 do
   begin
     ok:= calculatemoleculardescriptorsonmolecule(molecule_i);
     if not ok then
       writeln('time exceeded');
   end;
end;

calculatemoleculardescriptorsonmolecule can takes a lot of time (from 
few milliseconds to hours, if the molecule is huge or complex), I would 
like to put a user defined time limit to the calculation on a single 
molecule, if the time limit is reached the app should pass to the next 
molecule.
so the timer should check, during calculations, if the time per molecule 
is reached, then stop the actual molecule calculation.

andrea






More information about the Lazarus mailing list