Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Pascal Delphi Misc > Re: Automatic p...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 3 of 4 Topic 6072 of 6160
Post > Topic >>

Re: Automatic processing after form shows

by Rob Kennedy <me3@[EMAIL PROTECTED] > Jun 12, 2008 at 08:41 PM

milleratotago wrote:
> On Jun 13, 3:10 am, Dale.Ing...@[EMAIL PROTECTED]
 wrote:
>> I have several child forms that open as modal dialogs; I fill them in
(they're all listviews) in the OnShow event handlers, but sometimes the
processing can take a long time, so the user (me) sits there staring at
nothing for quite a while until the form suddenly pops up all filled in or
processed.
> 
> Here's a dirty trick--pretty ugly, but it works.
> 
> 1. Put a timer on the form with a very small interval,
> and set it's enabled to false.
> 
> 2. In FormShow, just enable the timer.
> 
> 3. Put the code to load up the form into the OnTimer event
> handler (and be sure to set its enabled back to false here).

The essence of the dirty trick is that the timer message won't be 
handled until the program's message loop begins or resumes running.

The part that makes it dirty is that you're using a timer for something 
that's obviously not periodic, and then the timer sticks around for the 
duration of the form's lifetime even though there's nothing to time.

To resolve that, take out the timer and just use a message. It doesn't 
need to be a timer message. Any message will do. Use PostMessage to put 
the message on the end of the queue, _after_ all the messages that have 
already been queued to make the form appear. When the message loop runs, 
that message will work its way to the front of the queue.

Define a message:

const
   am_InitForm = wm_User + 1;

Then change the Timer1Timer method to handle the message instead:

private
   procedure AMInitForm(var Message: TMessage); message am_InitForm;

> Here is a small example with just a memo and a timer on the form:
> 
> procedure TForm1.FormShow(Sender: TObject);
> Var i : Integer;
> begin
> Timer1.Enabled := True;

Instead of enabling the timer, post the message:

PostMessage(Handle, am_InitForm, 0, 0);

> {
> If you include this code here (without the
> timer), then the form only shows up after
> 20 sec and the memo already has its 10 lines.
> For i := 1 to 10 Do Begin
>   Memo1.Lines.Add(IntToStr(i));
>   Sleep(2000);
>   End;
> }
> end;
> 
> procedure TForm1.Timer1Timer(Sender: TObject);

Rename this and change the signature to match AMInitForm above.

> Var I : integer;
> begin
> Timer1.Enabled := False;  // Don't forget this one!

No need to disable the timer anymore. The very fact that you don't post 
another am_InitForm message will be sufficient to prevent initialization 
from occurring again.

> { If you load up the memo with this timer event handler,
> then the form will show immediately and the new lines
> will appear every 2 sec. }
> For i := 1 to 10 Do Begin
>   Memo1.Lines.Add(IntToStr(i));
>   Sleep(2000);
>   End;
> End;
> 
> Hope that helps,
> 


-- 
Rob
 




 4 Posts in Topic:
Automatic processing after form shows
Dale.Ingold@[EMAIL PROTEC  2008-06-12 11:10:06 
Re: Automatic processing after form shows
milleratotago <millera  2008-06-12 18:20:58 
Re: Automatic processing after form shows
Rob Kennedy <me3@[EMAI  2008-06-12 20:41:47 
Re: Automatic processing after form shows
Dale.Ingold@[EMAIL PROTEC  2008-06-13 11:24:09 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Tue Oct 14 9:47:03 CDT 2008.