[Lazarus] Lazarus and Application.ProcessMessages

Lukasz Sokol el.es.cr at gmail.com
Fri Dec 5 14:59:15 CET 2014


On 04/12/14 20:34, waldo kitty wrote:
> On 12/4/2014 10:24 AM, Lukasz Sokol wrote:
>> (OnTimer execution has the same problem : inevitably splits the procedure into
>> more procedures, that can no longer use same local variables and stuff)
> 
> you can't make them subroutines of the subroutine?
> 

Interesting, but breaks the flow of human reading (as, comparably, top posting does):
because subroutines have to be defined at the top of the subroutine...

> eg:
> 
> program foobie;
> 
> procedure dofoobie;
> 
> var
>   thisfoo : integer = 0;
>   thatfoo : integer = 0;
> 
>   procedure littlefoobie; ///  << this logically comes AFTER but we don't know that until we read...
>   begin
>     inc(thisfoo,thatfoo);
>     inc(thatfoo);         /// this probably is not much of a problem when a subroutine is this short
                            // it's the much-longer ones 
>   end;
> 
> begin
>   repeat
>     begin
	foobiee( ... ); /// ... that this logically comes first
>       littlefoobie;
>       writeln(thisfoo,'    ',thatfoo);
>     end;
>   until thisfoo >= 25;
> end;
> 
> begin
>   dofoobie;
> end.
> 
> 

How would you put this into subroutines:


procedure very_long_initialization_routine;
// (var some_big_list_of_variables... )

begin
  Timer1.Enabled := false;
 
  do_this();
  do_that();  // now for the sake of argument, say this routine fires up a thread reading in 10000s of file names through FindFirst/FindNext
  // now we need a long delay before calling do_foo() that would operate on that list, so
 
  Timer1.OnTimer := do_the_rest_of_initialization();
  Timer1.Delay   := 10000; // pseudo-code, can't remember how was that exactly called
  Timer1.Enabled := true;
  // that's it, we need to split here to give message loop opportunity to do Application.ProcessMessages
end;

/// (and here we break variable locality...)

procedure do_the_rest_of_initialization;
begin
  do_foo(...);
  // rinse&repeat if you have more sections like that doing different things...
end;

One can of course have TTimer as locally created object in the very_long_initialization_routine, yes... but an
Application.ProcessMessages + sleep(100) loop is probably much easier to type (and debug, and read).

Not mentioning that probably loop would be a wee little (tiny) bit more precise than calling the Timer ?

(that of course YMMV and IMvhO)

el_es





More information about the Lazarus mailing list