Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

delphi - What is the best way to autostart an action after OnShow event?

I have a small application that most of the time have an action behind a Start-button that should be triggered from the commandline parameter /AUTORUN. If that parameter is missing the user could also press it manually.

My question is where should I place this check for commandline so when it is given the GUI is still updated. The current solution is this, but the GUI is not updated until the action is finished.

procedure TfrmMainForm.FormShow(Sender: TObject);
begin
  if FindCmdLineSwitch('AUTORUN') then
    btnStart.Click;
end;
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Post yourself a message from your OnShow event handler. This will be processed as soon as your application starts servicing its message queue. That only happens when the application is ready to receive input. Which matches your my understanding of your requirements.

const
  WM_STARTUP = WM_USER;
....
procedure TfrmMainForm.FormShow(Sender: TObject);
begin
  PostMessage(Handle, WM_STARTUP, 0, 0);
  OnShow := nil;//only ever post the message once
end;

Add a message handler to deal with the message:

procedure WMStartup(var Msg: TMessage); message WM_STARTUP;

You'd implement that like this:

procedure TfrmMainForm.WMStartup(var Msg: TMessage);
begin
  inherited;
  if FindCmdLineSwitch('AUTORUN') then
    btnStart.Click;
end;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...