Rainer von Saleski wrote:
> The problem is that the work happens before the form is, well, "shown".
> You need to get the form established before it can close.
>
> My solution is to use a TTimer.  Have OnActivate or OnShow enable a
> TTimer, and move the work to the TTimer event.  It doesn't have to be a
> very long time delay -- just a few milliseconds -- but this gets the
> working code out of the form setup phase.

The reason a timer works is that it causes a wm_Timer message to be posted
to your application's message queue, and that only gets processed after
existing messages (such as the one that triggered OnActivate and the ones
that will trigger further form-showing messages) have been processed.

Since all you really need is one message, and you want it to be processed
as soon as possible after the form-showing messages are finished, you can
dispense with a timer and simply post your form a message:

const
  m_DeleteFiles = wm_User + 1;

type
  TFileDeletionForm = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    procedure MDeleteFiles(const Msg: TMessage); message m_DeleteFiles;
  end;

procedure TFileDeletionForm.FormCreate(Sender: TObject);
begin
  PostMessage(Handle, m_DeleteFiles, 0, 0);
end;

Using a timer is a little easier, but it also suggests to anyone taking a
cursory glance at your code that you intend to have code run periodically
instead of just once, and it also suggests that the timer interval is
significant when it really isn't.

On the other hand, if this form is really being used to delete files with
a progress bar, then ShFileOperation might be the better way to go. Give
it a list of files, and it will delete them while displaying a progress
dialog.

-- 
Rob


_______________________________________________
Delphi mailing list -> [email protected]
http://www.elists.org/mailman/listinfo/delphi

Reply via email to