<div dir="ltr"><div>It shouldn't be puzzling. It is not the fact that the form is starting minimized, but the fact that Application.Run varies it's behaviour for the SW_SHOWMINNOACTIVE value CmdShow, a variation which does not occur for SW_MINIMIZED.</div>
<div><br></div><div><br></div>But NOOOOOoooooo - do NOT add calls to ProcessMessages(). Do not add ANY calls to ProcessMessages(). And remove any you already have !!! :)<br><br>ProcessMessages() calls make your application UI code re-entrant in highly unpredictable ways that can have you forever trying to find Heisenbugs, especially in a case like this where a user is likely to click on your launched app icon to see if it has "woken up" yet.<br>
<br>Do blocking work on a background thread, allow the UI to continue to respond on the main thread, and provide whatever feedback to the UI relating to the progress of the background thread via properly marshalled, thread safe mechanisms.<br>
<br><br>You will get more sleep that way. :)<br><br></div><div class="gmail_extra"><br><br><div class="gmail_quote">On 25 July 2014 12:43, John Bird <span dir="ltr"><<a href="mailto:johnkbird@paradise.net.nz" target="_blank">johnkbird@paradise.net.nz</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div dir="ltr">
<div dir="ltr">
<div style="FONT-SIZE:12pt;FONT-FAMILY:'Calibri';COLOR:#000000">
<div>It looks like the code in D2007 Application.Run is the later
one. I would like to solve this at some stage out of curiosity so
will return. The SW_SHOWMINIMIZED works as expected which is a
puzzle. Usual story however, this is not on the list of urgent things to
get working so it has to be put aside for now. </div>
<div> </div>
<div>Its a good point you make about where to put startup code – my rule of
thumb has generally been initialising non component stuff can be done in the
form create, but I tend to put anything initialising component stuff on the form
show event, and this is where much of it is – there is a bit of setting things
visible or not depending on what properties are set and I don’t think this can
generally be done before the Show event. This Program B does this
general initialisation and then starts a timer which then fires of the rest of
the startup code which is long running. Some of the long running
code takes 20-40 seconds to run (calculations) and it doesn’t respond to
minimising/maximising while that is happening which is another minor issue – I
may need to put some extra processmessages calls in there eventually.</div>
<div> </div>
<div>This relates to a previous question – the long running calculation sets up
some quite large arrays and I tried saving/reading them in from disk to bypass
the calculations but it produced a stack overflow. I am presuming that this is
not from the code, as Russell’s code to save and load arrays to disk ran
standalone, but due to the program using a larger amount of memory.
Another investigation for later on the list of priorities!</div>
<div>
<div style="FONT-SIZE:small;TEXT-DECORATION:none;FONT-FAMILY:"Calibri";FONT-WEIGHT:normal;COLOR:#000000;FONT-STYLE:normal;DISPLAY:inline"><font size="3" face="Calibri"></font></div>
<div style="FONT:10pt tahoma">
<div style="BACKGROUND:#f5f5f5">
<div><b>From:</b> <a title="jsmith@deltics.co.nz" href="mailto:jsmith@deltics.co.nz" target="_blank">Jolyon Smith</a> </div>
<div><b>Sent:</b> Friday, July 25, 2014 7:49 AM</div><div class="">
<div><b>To:</b> <a title="delphi@listserver.123.net.nz" href="mailto:delphi@listserver.123.net.nz" target="_blank">NZ Borland Developers Group - Delphi
List</a> </div>
</div><div><b>Subject:</b> Re: [DUG] Shellexecute question</div></div></div>
<div> </div></div>
<div style="FONT-SIZE:small;TEXT-DECORATION:none;FONT-FAMILY:"Calibri";FONT-WEIGHT:normal;COLOR:#000000;FONT-STYLE:normal;DISPLAY:inline">
<div dir="ltr"><div><div class="h5">If the other Delphi application isn't correctly responding to
<b>SW_SHOWMINNOACTIVE</b> then the problem is in the <b>TApplicaiton</b> code of
the version of Delphi involved. The fact that your FormShow event isn't
firing suggests that it's an older version of Delphi involved, since an
inspection of the <b>TApplication.Run</b> method reveals how this behaviour (or
lack of) would eventuate.... <br><br>
<div> case CmdShow of</div>
<div><b> SW_SHOWMINNOACTIVE:
FMainForm.FWindowState := wsMinimized;</b></div>
<div> SW_SHOWMAXIMIZED:
MainForm.WindowState := wsMaximized;</div>
<div> end;</div>
<div> if FShowMainForm then</div>
<div><b> if FMainForm.FWindowState =
wsMinimized then</b></div>
<div><b> Minimize
else</b></div>
<div> FMainForm.Visible :=
True;</div>
<div> </div>
<div>The internal state is forced to <b>wsMinimize, </b>bypassing normal
property setters, resulting in the "show" code simply calling the
<b>Minimize</b> method on the main form, rather than making the form visible,
which is why the FormShow even isn't fired. The form isn't shown!
i.e. the <b>FormShow</b> event will eventually fire only when the user first
activates the application and the window becomes shown. You could argue
that this is desirable behaviour, but doesn't suit your purposes in this
case.</div>
<div> </div>
<div>This was changed in later versions of Delphi. The above code is from
D7. The version below from XE4 (the only 2 versions I have available on
this system):</div>
<div> </div>
<div>
<div> case CmdShow of</div>
<div><b> SW_SHOWMINNOACTIVE:</b></div>
<div><b> begin</b></div>
<div><b>
FInitialMainFormState := wsMinimized;</b></div>
<div><b>
FMainForm.FWindowState := wsMinimized;</b></div>
<div><b> end;</b></div>
<div> SW_SHOWMAXIMIZED:
MainForm.WindowState := wsMaximized;</div>
<div> end;</div>
<div> if FShowMainForm then</div>
<div><b> if (FMainForm.FWindowState =
wsMinimized) or (FInitialMainFormState = wsMinimized) then</b></div>
<div><b> begin</b></div>
<div><b>
Minimize;</b></div>
<div><b> if
(FInitialMainFormState = wsMinimized) then</b></div>
<div><b>
FMainForm.Show;</b></div>
<div> end else</div>
<div> FMainForm.Visible :=
True;</div></div>
<div> </div>
<div>Now it appears that the <b>Show</b> method of your main form should be
called, though I haven't tested to see whether this is actually the case.
But in theory, <b>SW_SHOWMINNOACTIVE</b> should work for you if you upgrade the
target EXE to a more current version of Delphi.</div>
<div> </div>
<div>Alternatively, you could move the initialisation code in that EXE,
currently triggered by the FormShow event, to a more appropriate initialisation
event. After all, this is initialisation that needs to be performed
regardless of whether the form is <b>Show'</b>n or not. ;)</div>
<div> </div>
<div>It's possible that some of that code cannot be performed in
<b>FormCreate</b>, which is a quite common reason to use <b>FormShow</b>
instead. If that's the case, then one possible solution is to <b>post</b>
a custom message to yourself in <b>FormCreate</b>, to fabricate the
create-deferred event you need, without relying on the form being shown to
generate the <b>FormShow</b> event:<br><br><br>const</div>
<div> MM_INITIALISE = WM_USER + 1;</div>
<div> </div>
<div> </div>
<div>TMyForm = class(TForm)</div>
<div> ...</div>
<div> procedure MMInitialise(var aMessage: TMessage); message
MM_INITIALISE;</div>
<div>end;</div>
<div> </div>
<div> </div>
<div>TMyForm.FormOnCreate...<br></div>
<div>begin</div>
</div></div><div> PostMessage(Handle, MM_INITIALISE, 0, 0);<br>
....</div><div><div class="h5">
<div>end;</div>
<div> </div>
<div> </div>
<div>
<div>procedure TMyForm.MMInitialise(var aMessage: TMessage); </div></div>
<div>begin</div>
<div> // Do initialisation here...</div>
<div>end;</div>
<div> </div>
<div> </div>
<div> </div>
<div>Good luck</div></div></div></div><div><div class="h5">
<div class="gmail_extra"><br><br>
<div class="gmail_quote">On 25 July 2014 00:22, John Bird <span dir="ltr"><<a href="mailto:johnkbird@paradise.net.nz" target="_blank">johnkbird@paradise.net.nz</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="PADDING-LEFT:1ex;MARGIN:0px 0px 0px 0.8ex;BORDER-LEFT:#ccc 1px solid">
<div dir="ltr">
<div dir="ltr">
<div style="FONT-SIZE:12pt;FONT-FAMILY:'Calibri';COLOR:#000000">
<div>Hey that should have been perfect, as I already was doing
SW_SHOWMINIMIZED which works fine, and so does SW_SHOWNOACTIVE which
also does what it should – start the other window but not change focus.</div>
<div> </div>
<div>However SW_SHOWMINNOACTIVE doesn’t work. Now the other
program is a Delphi program of mine that does some initialisation in the
Formshow event (turning on timers etc) and I am wondering if somehow the event
doesn’t fire. The process starts, but nothing runs, looks asleep in Task
Manager. Not worth messing around with, as its a minor issue.</div>
<div><font face="Calibri"></font> </div>
<div>I toyed with using SW_SHOWINACTIVE and getting the program (Program B) to
minimise itself on start, but that is just damned complicated and
fiddly/fragile. It also seems prone to ending up with 2 icons on the
task bar, as though multiple copies have started even if only one is running –
(maybe due to the large amount of work it does on startup – it is unresponsive
for a good while) .</div>
<div> </div>
<div><font face="Calibri">I also tried Russells suggestion about setting the
foreground window and it don’t work for me (Windows 8.1)</font></div>
<div> </div>
<div>What I did in the end was go back to the SW_SHOWMINIMIZED and then after
the ShellExecute (in Program A) I put a ShowMessage saying I had started the
other program. Because this gives a modal clue that they have to
click on to continue it will do the job of setting the focus back. And
its a bit useful for them to be informed it has been started.</div>
<div style="FONT-SIZE:small;TEXT-DECORATION:none;FONT-FAMILY:"Calibri";FONT-WEIGHT:normal;COLOR:#000000;FONT-STYLE:normal;DISPLAY:inline">
<div style="FONT:10pt tahoma">
<div><font size="3" face="Calibri"></font> </div>
<div><font size="3" face="Calibri"></font> </div>
<div style="BACKGROUND:#f5f5f5">
<div><b>From:</b> <a title="jsmith@deltics.co.nz" href="mailto:jsmith@deltics.co.nz" target="_blank">Jolyon Smith</a> </div>
<div><b>Sent:</b> Thursday, July 24, 2014 8:21 PM</div>
<div><b>To:</b> <a title="russell@belding.co.nz" href="mailto:russell@belding.co.nz" target="_blank">Russell Belding</a> ; <a title="delphi@listserver.123.net.nz" href="mailto:delphi@listserver.123.net.nz" target="_blank">NZ Borland Developers Group - Delphi List</a> </div>
<div><b>Subject:</b> Re: [DUG] Shellexecute question</div></div></div>
<div> </div></div>
<div style="FONT-SIZE:small;TEXT-DECORATION:none;FONT-FAMILY:"Calibri";FONT-WEIGHT:normal;COLOR:#000000;FONT-STYLE:normal;DISPLAY:inline">
<div>
<div>
<div dir="ltr">Have you tried passing <strong style="FONT-SIZE:13px;FONT-FAMILY:'Segoe UI','Lucida Grande',verdana,arial,helvetica,sans-serif;COLOR:rgb(42,42,42);LINE-HEIGHT:18px">SW_SHOWMINNOACTIVE
instead of SW_MINIMIZED ?</strong>
<div><span style="FONT-SIZE:13px;FONT-FAMILY:'Segoe UI','Lucida Grande',verdana,arial,helvetica,sans-serif;COLOR:rgb(42,42,42);LINE-HEIGHT:18px"><br></span></div>
<div><span style="FONT-SIZE:13px;FONT-FAMILY:'Segoe UI','Lucida Grande',verdana,arial,helvetica,sans-serif;COLOR:rgb(42,42,42);LINE-HEIGHT:18px">Caveat:
The show flag parameter is merely passed to the application being
executed. What it chooses to do with that flag is it's own affair, but
if you're lucky, it will respect your wishes. If not, then you will have
to engage in a focus window arms race/lotto as already suggested. But
Route #1 would be to try the officially mandated mechanisms.<br><br>Good
luck. :)</span></div></div>
<div class="gmail_extra"><br><br>
<div class="gmail_quote">On 24 July 2014 19:46, russell <span dir="ltr"><<a href="mailto:russell@belding.co.nz" target="_blank">russell@belding.co.nz</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="PADDING-LEFT:1ex;MARGIN:0px 0px 0px 0.8ex;BORDER-LEFT:#ccc 1px solid">
<div lang="EN-NZ" bgcolor="white" vlink="purple" link="blue">
<div>
<p class="MsoNormal"><span style="FONT-SIZE:11pt;FONT-FAMILY:"Calibri","sans-serif";COLOR:#1f497d">Tyr
this after spawning the other program.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="FONT-SIZE:11pt;FONT-FAMILY:"Calibri","sans-serif";COLOR:#1f497d"><u></u><u></u></span> </p>
<p class="MsoNormal"><span style="FONT-SIZE:11pt;FONT-FAMILY:"Calibri","sans-serif";COLOR:#1f497d">SetForegroundWindow(forms.application.mainWindow.handle)<u></u><u></u></span></p>
<p class="MsoNormal"><span style="FONT-SIZE:11pt;FONT-FAMILY:"Calibri","sans-serif";COLOR:#1f497d"><u></u><u></u></span> </p>
<p class="MsoNormal"><span style="FONT-SIZE:11pt;FONT-FAMILY:"Calibri","sans-serif";COLOR:#1f497d">To
give the main window of your program focus.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="FONT-SIZE:11pt;FONT-FAMILY:"Calibri","sans-serif";COLOR:#1f497d">Perhaps
modifications of this will take you to the window of the calling program
where you want the focus?<u></u><u></u></span></p>
<p class="MsoNormal"><span style="FONT-SIZE:11pt;FONT-FAMILY:"Calibri","sans-serif";COLOR:#1f497d"><u></u><u></u></span> </p>
<p class="MsoNormal"><span style="FONT-SIZE:11pt;FONT-FAMILY:"Calibri","sans-serif";COLOR:#1f497d">Russell<u></u><u></u></span></p>
<p class="MsoNormal"><span style="FONT-SIZE:11pt;FONT-FAMILY:"Calibri","sans-serif";COLOR:#1f497d"><u></u><u></u></span> </p>
<div>
<div style="BORDER-TOP:#b5c4df 1pt solid;BORDER-RIGHT:medium none;BORDER-BOTTOM:medium none;PADDING-BOTTOM:0cm;PADDING-TOP:3pt;PADDING-LEFT:0cm;BORDER-LEFT:medium none;PADDING-RIGHT:0cm">
<p class="MsoNormal"><b><span lang="EN-US" style="FONT-SIZE:10pt;FONT-FAMILY:"Tahoma","sans-serif";COLOR:windowtext">From:</span></b><span lang="EN-US" style="FONT-SIZE:10pt;FONT-FAMILY:"Tahoma","sans-serif";COLOR:windowtext">
<a href="mailto:delphi-bounces@listserver.123.net.nz" target="_blank">delphi-bounces@listserver.123.net.nz</a> [mailto:<a href="mailto:delphi-bounces@listserver.123.net.nz" target="_blank">delphi-bounces@listserver.123.net.nz</a>] <b>On Behalf Of
</b>John Bird<br><b>Sent:</b> Thursday, 24 July 2014 4:43 p.m.<br><b>To:</b>
NZ Borland Developers Group - Delphi List<br><b>Subject:</b> [DUG]
Shellexecute question<u></u><u></u></span></p></div></div>
<div>
<div>
<p class="MsoNormal"><u></u><u></u> </p>
<div>
<div>
<div>
<p class="MsoNormal"><span style="FONT-SIZE:10pt;FONT-FAMILY:"Tahoma","sans-serif"">I have a program
(Program A) that fires up another (program B) via ShellExecute, if its
not already running. However even though Program B is started
minimised, focus shifts away from Program A, which is a minor
nuisance.</span><span style="FONT-FAMILY:"Calibri","sans-serif""><u></u><u></u></span></p></div>
<div>
<p class="MsoNormal"><span style="FONT-FAMILY:"Calibri","sans-serif""><u></u><u></u></span> </p></div>
<div>
<p class="MsoNormal"><span style="FONT-SIZE:10pt;FONT-FAMILY:"Tahoma","sans-serif"">Is there any way
to stop this within Delphi? Or will I have to do something like delve
into the Windows API?</span><span style="FONT-FAMILY:"Calibri","sans-serif""><u></u><u></u></span></p></div>
<div>
<p class="MsoNormal"><span style="FONT-FAMILY:"Calibri","sans-serif""><u></u><u></u></span> </p></div>
<div>
<p class="MsoNormal"><span style="FONT-FAMILY:"Calibri","sans-serif"">if
ShellExecute(Application.Mainform.Handle, 'open', Pchar(aProgName),
PChar(aparaml), PChar(aDir), SW_SHOWMINIMIZED) <= 32
then<u></u><u></u></span></p></div>
<div>
<p class="MsoNormal"><span style="FONT-FAMILY:"Calibri","sans-serif"">
ShowMessage('Start Minimised error:')<u></u><u></u></span></p></div>
<div>
<p class="MsoNormal"><span style="FONT-FAMILY:"Calibri","sans-serif""><u></u><u></u></span> </p></div></div></div></div></div></div></div><br>_______________________________________________<br>NZ
Borland Developers Group - Delphi mailing list<br>Post: <a href="mailto:delphi@listserver.123.net.nz" target="_blank">delphi@listserver.123.net.nz</a><br>Admin: <a href="http://delphi.org.nz/mailman/listinfo/delphi" target="_blank">http://delphi.org.nz/mailman/listinfo/delphi</a><br>
Unsubscribe:
send an email to <a href="mailto:delphi-request@listserver.123.net.nz" target="_blank">delphi-request@listserver.123.net.nz</a> with Subject:
unsubscribe<br></blockquote></div>
<div> </div></div></div></div>
<hr>
<div>_______________________________________________<br>NZ Borland Developers
Group - Delphi mailing list<br>Post: <a href="mailto:delphi@listserver.123.net.nz" target="_blank">delphi@listserver.123.net.nz</a><br>Admin: <a href="http://delphi.org.nz/mailman/listinfo/delphi" target="_blank">http://delphi.org.nz/mailman/listinfo/delphi</a><br>
Unsubscribe:
send an email to <a href="mailto:delphi-request@listserver.123.net.nz" target="_blank">delphi-request@listserver.123.net.nz</a> with Subject:
unsubscribe</div></div></div></div></div><br>_______________________________________________<br>NZ
Borland Developers Group - Delphi mailing list<br>Post: <a href="mailto:delphi@listserver.123.net.nz" target="_blank">delphi@listserver.123.net.nz</a><br>Admin:
<a href="http://delphi.org.nz/mailman/listinfo/delphi" target="_blank">http://delphi.org.nz/mailman/listinfo/delphi</a><br>Unsubscribe:
send an email to <a href="mailto:delphi-request@listserver.123.net.nz" target="_blank">delphi-request@listserver.123.net.nz</a>
with Subject: unsubscribe<br></blockquote></div>
<div> </div></div>
<p>
</p><hr>
_______________________________________________<br>NZ Borland Developers Group -
Delphi mailing list<br>Post: <a href="mailto:delphi@listserver.123.net.nz" target="_blank">delphi@listserver.123.net.nz</a><br>Admin:
<a href="http://delphi.org.nz/mailman/listinfo/delphi" target="_blank">http://delphi.org.nz/mailman/listinfo/delphi</a><br>Unsubscribe: send an email to
<a href="mailto:delphi-request@listserver.123.net.nz" target="_blank">delphi-request@listserver.123.net.nz</a> with Subject:
unsubscribe<p></p></div></div></div></div></div></div>
<br>_______________________________________________<br>
NZ Borland Developers Group - Delphi mailing list<br>
Post: <a href="mailto:delphi@listserver.123.net.nz">delphi@listserver.123.net.nz</a><br>
Admin: <a href="http://delphi.org.nz/mailman/listinfo/delphi" target="_blank">http://delphi.org.nz/mailman/listinfo/delphi</a><br>
Unsubscribe: send an email to <a href="mailto:delphi-request@listserver.123.net.nz">delphi-request@listserver.123.net.nz</a> with Subject: unsubscribe<br></blockquote></div><br></div>