[DUG] Shellexecute question
russell
russell at belding.co.nz
Fri Jul 25 16:44:29 NZST 2014
Hi Jolyon
This doesn't really establish anything other than what we already know
With respect, how about toning down your strong statements?
I was not trying to establish something, just looking for a pragmatic solution to a problem John posed.
Anything else is built on an assumption or set of assumptions that may or may not be valid at any given time.
Overstated? Consider that we often start one program after another and each program might run several threads. Such actions have a goal of starting two programs and this works AFAIK all the time.
Thanks for your insight on threading. There still may be a simple pragmatic solution to John’s problem.
J
Russell
From: Jolyon Smith [mailto:jsmith at deltics.co.nz]
Sent: Friday, 25 July 2014 4:29 p.m.
To: Russell Belding; NZ Borland Developers Group - Delphi List
Subject: Re: [DUG] Shellexecute question
This doesn't really establish anything other than what we already know... that it is incredibly difficult to reliably anticipate in one thread something that is happening in some other thread.
Prog A > launch Prog B
In simplistic terms you now have two threads, let's call them Thread A and B (corresponding to each program, A and B). If Thread A now does something that might interfere with/be interefered with by something occuring on Thread B then if you want any sort of reliable behaviour there has to be some co-ordination between the threads. Anything else is built on an assumption or set of assumptions that may or may not be valid at any given time.
In this case, if Thread A tries to SetForegroundWindow() because it believes that Thread B has "stolen" it, then it's attempt to steal it back is only going to succeed if the call to SetForegroundWindow() occurs AFTER Thread B has in fact stolen it. If Thread A reclaims the focus BEFORE Thread B has nicked it, then Trehad B is still going to end up stealing the focus.
Thread A can build in an assumption that if it waits a certain amount of time then Thread B will probably have completed it's stealing activity, but this is still an assumption and is just as vulnerable to failure.
On 25 July 2014 16:17, russell <russell at belding.co.nz> wrote:
I also tried Russells suggestion about setting the foreground window and it don’t work for me (Windows 8.1)
I tested this earlier on W7 by starting another program, so my main program lost focus. My main program was triggering
SetForegroundWindow(forms.application.mainWindow.handle)
from a timer and it worked OK. That is, focus returned to my main program when the timer went off.
Just now I ran
bcdShellExecute(<path toExcel>);
SetForegroundWindow(forms.application.mainWindow.handle);
and focus was not returned to my main program, it stayed with Excel.
As a guess I tried
bcdShellExecute(<path toExcel>);
Sleep(150);
SetForegroundWindow(forms.application.mainWindow.handle);
and focus was returned to my main program. We can each guess what this means about interrupting a starting program in W7.
I tried a similar test on W8.1 and focus is not returned to my main program even using sleep(1250).
R
From: delphi-bounces at listserver.123.net.nz [mailto:delphi-bounces at listserver.123.net.nz] On Behalf Of John Bird
Sent: Friday, 25 July 2014 12:22 a.m.
To: NZ Borland Developers Group - Delphi List
Subject: Re: [DUG] Shellexecute question
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.
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.
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) .
I also tried Russells suggestion about setting the foreground window and it don’t work for me (Windows 8.1)
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.
From: Jolyon Smith <mailto:jsmith at deltics.co.nz>
Sent: Thursday, July 24, 2014 8:21 PM
To: Russell Belding <mailto:russell at belding.co.nz> ; NZ Borland Developers Group - Delphi List <mailto:delphi at listserver.123.net.nz>
Subject: Re: [DUG] Shellexecute question
Have you tried passing SW_SHOWMINNOACTIVE instead of SW_MINIMIZED ?
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.
Good luck. :)
On 24 July 2014 19:46, russell <russell at belding.co.nz> wrote:
Tyr this after spawning the other program.
SetForegroundWindow(forms.application.mainWindow.handle)
To give the main window of your program focus.
Perhaps modifications of this will take you to the window of the calling program where you want the focus?
Russell
From: delphi-bounces at listserver.123.net.nz [mailto:delphi-bounces at listserver.123.net.nz] On Behalf Of John Bird
Sent: Thursday, 24 July 2014 4:43 p.m.
To: NZ Borland Developers Group - Delphi List
Subject: [DUG] Shellexecute question
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.
Is there any way to stop this within Delphi? Or will I have to do something like delve into the Windows API?
if ShellExecute(Application.Mainform.Handle, 'open', Pchar(aProgName), PChar(aparaml), PChar(aDir), SW_SHOWMINIMIZED) <= 32 then
ShowMessage('Start Minimised error:')
_______________________________________________
NZ Borland Developers Group - Delphi mailing list
Post: delphi at listserver.123.net.nz
Admin: http://delphi.org.nz/mailman/listinfo/delphi
Unsubscribe: send an email to delphi-request at listserver.123.net.nz with Subject: unsubscribe
_____
_______________________________________________
NZ Borland Developers Group - Delphi mailing list
Post: delphi at listserver.123.net.nz
Admin: http://delphi.org.nz/mailman/listinfo/delphi
Unsubscribe: send an email to delphi-request at listserver.123.net.nz with Subject: unsubscribe
_______________________________________________
NZ Borland Developers Group - Delphi mailing list
Post: delphi at listserver.123.net.nz
Admin: http://delphi.org.nz/mailman/listinfo/delphi
Unsubscribe: send an email to delphi-request at listserver.123.net.nz with Subject: unsubscribe
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://listserver.123.net.nz/pipermail/delphi/attachments/20140725/f78b79d9/attachment-0001.html
More information about the Delphi
mailing list