PDA

View Full Version : Multi Instance at runtime Handler code


SUF6NEWBIE
10-19-2004, 04:24 AM
Here's an 'unrefined' ..could do with some Guru Help-refinement..

This is really just an adaptation of BRETTS example script.

Global Function to detect if 'mysetup' is already running and if so Exit.

function FindAndCloseProcessByName(strName, strnum)
local tblProcesses = Window.EnumerateProcesses(true);
local bProcessFound = false;
local nProccess = 0; --set intitial count to zero
local nProcessHandle = nil;
if(tblProcesses)then
local strProcessName;
local nHandle;
for nHandle, strProcessName in tblProcesses do
if(String.Find(strProcessName, strName, 1, false) ~= -1)then
nProcessHandle = nHandle;
nProccess = nProccess + 1;
bProcessFound = true;
end
end
end
if(bProcessFound and nProcessHandle)then
Dialog.Message("Total tgt Proccess", nProccess); --debug dialog only
if (nProccess >= strnum) then
Application.Exit(0); -- multi instance of mysetup detected so exit now
else
Window.Close(nProcessHandle,CLOSEWND_SENDMESSAGE);
end

end
end

to call it ..say in Startup actions...

FindAndCloseProcessByName("irsetup.exe", 2); -- Close App if 'irsetup' count > 1

Could others please check this out ...
(note - still can handle other proccess detection if called appropriately)
sorry about the 'layout' as displayed ...

tks

SUF6NEWBIE
10-19-2004, 04:39 AM
OOPS ! Change-remove the lines:

else
Window.Close(nProcessHandle,CLOSEWND_SENDMESSAGE);
end

to just:

end



I can see a 'limitation' ?..when a 'Dependancy Module' is part of 'mysetup'

the 'module script' runs Before any Global functions and Startup Actions.

I 'spose the Function could be included in the 'Detection Script' for a Module
as a way around this.

SUF6NEWBIE
10-19-2004, 05:54 AM
Here's a trimmed down test project you can use...
has the code ready to go upon build...

..should not alter your system..only a welcome screen and
a 'finish screen' ..no log files etc etc.

csd214
10-19-2004, 08:16 AM
SUF6NEWBIE, thanks for sharing your global script.

I encourage all SUF70 users to contribute to this forum LIKE THE AMS50 USERS DO. To really learn SUF70 you need:

1) The Help doc
2) Corey's Video Training CD
3) This Forum

The last element requires that the users participate with an open mind. I don't forget a function I published in the AMS Forum. It was a script with 44 lines (it worked and I was proud of it). Then Lorne stepped in and showed another way to do it: A script with 5 lines!! Don't miss the possibility to learn something new every day!

My contribution is a global script similar to SUF6NEWBIE'S, but I use the Windows Enumerate function and the session variable %ProductName%")

function CloseAppIfRuning(cpTitle)
--[[
function: CloseAppIfRuning()
Purpose: Stop runing the app if it is already started
Arguments: Window Title as SessionVar.Get("%ProductName%")
Returns: Nothing ::: CLOSES THE APP :::
Usage: CloseAppIfRuning(SessionVar.Get("%ProductName%")); (in 'On Startup')
]]

local tTitles = Window.EnumerateTitles(true); local bAppInUse = false; local nAppCount = 0;
for nfHandle, cfEnumTitle in tTitles do
if String.Find(cfEnumTitle, cpTitle, 1, false) ~= -1 then
nAppCount = nAppCount + 1;
if nAppCount > 1 then
bAppInUse = true;
end
end
end
if bAppInUse then
Dialog.Message("Error: In Use", "The application "..cpTitle.." is already started"
.."\r\nThis second occurrence will now be terminated", MB_OK, MB_ICONSTOP);
Application.Exit();
end
end

SUF6NEWBIE
10-19-2004, 08:39 AM
TKS CSD...the enumerate title is more like how I do it in AMS

also thought I would have a crack at the 'proccess way'

Yep the more we share the more we can learn from each other.

I'll fire up your code and give it a run..

SUF6NEWBIE
10-19-2004, 03:16 PM
CSD ...found the Code you posted to be a little faster and solves
issues surrounding other 'irsetup.exe" that may purposefully
be running..as only detects by Product Name.

Another CSD triumph !

tks

csd214
10-20-2004, 08:21 AM
Thx for the credit (I have to confess: I'm a bit unsure about the process stuff; I feel more comfortable with closing the app by the window title...)

SUF6NEWBIE
10-20-2004, 10:01 PM
Doing things the 'proccess way' is limited in the area that
SUF7 and AMS5 rely on 'window integer id' and Not the actual
'proccess id' ..(which is the OS way of doing things.)

eG: a window.close action will often close the windows, however
the actual target proccess is not 'released' by the OS, is in reality
still 'running in the background'. windows.close 'terminate' will often 'kill'
the process, however any created files or runtime files must be
manually deleted etc.

This could be viewed as a 'limitation in proccesss handling' - 'window id'

to fully 'kill' -release some proccesses an external resource is still required.
(again any created files etc must be manually deleted)

Controlling 'open Windows' etc using SUF7 actions etc is still powerfull
and very usefull, the more I get 'down and dirty' with it all.