Determining if a Macro is already running

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
jbas
Junior Coder
Posts: 29
Joined: Tue Dec 15, 2009 4:59 pm

Determining if a Macro is already running

Post by jbas » Tue Feb 04, 2020 2:16 am

. 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?

DreamTheater
Newbie
Posts: 19
Joined: Mon Oct 14, 2019 6:23 am

Re: Determining if a Macro is already running

Post by DreamTheater » Tue Feb 04, 2020 2:29 am

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>

jbas
Junior Coder
Posts: 29
Joined: Tue Dec 15, 2009 4:59 pm

Re: Determining if a Macro is already running

Post by jbas » Tue Feb 04, 2020 3:23 am

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!

User avatar
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

Post by Grovkillen » Tue Feb 04, 2020 3:42 am

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).
Let>ME=%Script%

Running: 15.0.27
version history

jbas
Junior Coder
Posts: 29
Joined: Tue Dec 15, 2009 4:59 pm

Re: Determining if a Macro is already running

Post by jbas » Tue Feb 04, 2020 3:56 am

That's it! Thank-you!

jbas
Junior Coder
Posts: 29
Joined: Tue Dec 15, 2009 4:59 pm

Re: Determining if a Macro is already running

Post by jbas » Tue Feb 04, 2020 4:02 am

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'?

User avatar
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

Post by Grovkillen » Tue Feb 04, 2020 7:15 am

See this thread here: viewtopic.php?f=8&t=10528
Let>ME=%Script%

Running: 15.0.27
version history

User avatar
JRL
Automation Wizard
Posts: 3526
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Re: Determining if a Macro is already running

Post by JRL » Tue Feb 04, 2020 2:21 pm

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.

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

jbas
Junior Coder
Posts: 29
Joined: Tue Dec 15, 2009 4:59 pm

Re: Determining if a Macro is already running

Post by jbas » Tue Feb 04, 2020 2:25 pm

That is excellent! Thank-you!

User avatar
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

Post by Grovkillen » Tue Feb 04, 2020 4:00 pm

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.
Let>ME=%Script%

Running: 15.0.27
version history

Scones
Junior Coder
Posts: 32
Joined: Fri Jul 05, 2019 11:21 am

Re: Determining if a Macro is already running

Post by Scones » Wed Feb 26, 2020 9:25 am

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

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts