Determining if a Macro is already running
Moderators: Dorian (MJT support), JRL
Determining if a Macro is already running
. I plan to distribute a compiled macro that contains several OnEvent commands. I need to ensure that the macro is not called iteratively. I was unable to find a better way to determine if the macro was already running so I'm writing a 'pulse' date / time to a .Ini file and and then checking to see if it is current.
. Is there a way to do this that does not require disk writes / reads?
. Is there a way to do this that does not require disk writes / reads?
-
- Newbie
- Posts: 19
- Joined: Mon Oct 14, 2019 6:23 am
Re: Determining if a Macro is already running
Have you tried something like this?
Code: Select all
ProcessExists>Process_Name.exe,res
If>res=True
MDL>Process is already running.
Exit>0
EndIf>
Re: Determining if a Macro is already running
If I distribute a macro with that code in it wouldn't is always just detect itself running? I'm not sure how that would help me see if a previous iteration of the macro was running. Thanks for the idea though!
- Grovkillen
- Automation Wizard
- Posts: 1131
- Joined: Fri Aug 10, 2012 2:38 pm
- Location: Bräcke, Sweden
- Contact:
Re: Determining if a Macro is already running
I usually add a ini file which will have a timestamp and a pid section. I post to this file and can easily check it upon start to see if another instance of the macro is already running. (Just check and see if the pid is running).
Re: Determining if a Macro is already running
That's it! Thank-you!
Re: Determining if a Macro is already running
Your suggestion definitely works for a compiled macro
Is there a way to get the process id for a macro running within macro scheduler in 'developer mode'?
Is there a way to get the process id for a macro running within macro scheduler in 'developer mode'?
- Grovkillen
- Automation Wizard
- Posts: 1131
- Joined: Fri Aug 10, 2012 2:38 pm
- Location: Bräcke, Sweden
- Contact:
Re: Determining if a Macro is already running
See this thread here: viewtopic.php?f=8&t=10528
Re: Determining if a Macro is already running
I like to use a dialog. If a dialog block is encountered the dialog becomes an existing window that can be detected. Put this code at the start of your script, if the window specified in the code already exists, a consecutive start of your executable will stop. If the running executable stops, the process test dialog window ceases to exist and the executable can then be started again.
Bottom line: Only one instance of your program can run on a given computer.
Bottom line: Only one instance of your program can run on a given computer.
Code: Select all
GetWindowProcess>UnmistakablyUniqueName202002040649,ProcID,ProcName
If>ProcID<>0
MDL>%Command_Line% is already running
Exit>0
EndIf
Dialog>DialogProcExistTest
object DialogProcExistTest: TForm
Caption = 'UnmistakablyUniqueName202002040649'
end
EndDialog>DialogProcExistTest
Re: Determining if a Macro is already running
That is excellent! Thank-you!
- Grovkillen
- Automation Wizard
- Posts: 1131
- Joined: Fri Aug 10, 2012 2:38 pm
- Location: Bräcke, Sweden
- Contact:
Re: Determining if a Macro is already running
I use a script which I call "watchdog" this one will read the ini file and can then independently of the other scripts know what pids to look at. If the heartbeat of a script isn't detected the watchdog will kill the process and start a new one. So in that sense my approach is more generic.
But I like JRLs idea too.
But I like JRLs idea too.
Re: Determining if a Macro is already running
Wouldn't this one also do the trick?
Code: Select all
GetProcessIds>My macro.exe,PID_ARR
ArrayCount>PID_ARR,PIDCOUNT
If>PIDCOUNT>1
MDL MACRO ALREADY RUNNING
exit
//Or insert dialog where user can choose to kill that existing process. (Will also kill this one ofc)
endif
//Continue macro