How to start or stop this script

Get help with using AutoHotkey and its commands and hotkeys
Nellybird

How to start or stop this script

19 Dec 2017, 05:18

taken from -- https://autohotkey.com/board/topic/5698 ... -notifier/
(specifcally mias post that is based on Lexikos code)

All I would like to do is have a gui that lets me start or start this script.

So far what I tried.

Removed #Persistent and then put it in a loop. The problem is I cannot get the loop to break. So i removed the loop idea. I tried to break the loop by adding to the start:

if (runit = 1)
break
And then made the stop button runit = 1 (I added in the script on top (runit =)

All I wish to have done is click start and it will run until I click the stop button? I do not think I ask so much.

Code: Select all

#SingleInstance Force
SetWorkingDir %A_ScriptDir%

Gui Add, Button, x134 y59 w80 h23 gScripStarts, Start 
Gui Add, Button, x136 y112 w80 h23 gScriptStop, Stop
Gui Font, cFuchsia, Segoe Script
Gui Add, Text, x96 y33 w120 h23 +0x200, Test of script
Gui Font

Gui Show, w481 h253, Window
Return


 
ScripStarts:
; ========== START OF SCRIPT ==========

 

; Get WMI service object.
winmgmts := ComObjGet("winmgmts:")

; Create sink objects for receiving event noficiations.
ComObjConnect(createSink := ComObjCreate("WbemScripting.SWbemSink"), "ProcessCreate_")
ComObjConnect(deleteSink := ComObjCreate("WbemScripting.SWbemSink"), "ProcessDelete_")

; Set event polling interval, in seconds.
interval := 2

; Register for process creation notifications:
winmgmts.ExecNotificationQueryAsync(createSink
    , "Select * from __InstanceCreationEvent"
    . " within " interval
    . " where TargetInstance isa 'Win32_Process'")

; Register for process deletion notifications:
winmgmts.ExecNotificationQueryAsync(deleteSink
    , "Select * from __InstanceDeletionEvent"
    . " within " interval
    . " where TargetInstance isa 'Win32_Process'")

; Don't exit automatically. I commented it out because I want to be able to run it to start and stop
;#Persistent

; Called when a new process is detected:
ProcessCreate_OnObjectReady(obj) {
    proc := obj.TargetInstance
    TrayTip New Process Detected, % "
    (LTrim
        ID:`t" proc.ProcessID "
        Parent:`t" proc.ParentProcessID "
        Name:`t" proc.Name "
        Path:`t" proc.ExecutablePath "
        
        Command line (requires XP or later):
        
        " proc.CommandLine
)

}

; Called when a process terminates:
ProcessDelete_OnObjectReady(obj) {
    proc := obj.TargetInstance
    TrayTip Process Terminated, % "
    (LTrim
        ID:`t" proc.Handle "
        Name:`t" proc.Name
)

}
 
 

; ========== END OF SCRIPT ==========

 

ScriptStop:
;what do i put here 
return


GuiEscape:
GuiClose:
    ExitApp
 
User avatar
boiler
Posts: 12991
Joined: 21 Dec 2014, 02:44

Re: How to start or stop this script

19 Dec 2017, 06:50

You should just use the Pause command associated with the buttons. Use Pause, On with the Start button and Pause, Off with the stop button. Or just have one button as a toggle with just Pause. You would need to then put Lexikos' script after the GUI stuff and before the return ending with a Pause command if you want its initial state to be "stopped". Then your button routines and hotkeys would be the only things after the return that ends the auto-execute section.
Rindis
Posts: 213
Joined: 23 Dec 2013, 13:58
Location: Norway

Re: How to start or stop this script

19 Dec 2017, 06:53

extremely quick and dirty:

Code: Select all

#SingleInstance Force
SetWorkingDir %A_ScriptDir%

Gui Add, Button, x134 y59 w80 h23 gScripStarts, Start 
Gui Add, Button, x136 y112 w80 h23 gScriptStop, Stop
Gui Font, cFuchsia, Segoe Script
Gui Add, Text, x96 y33 w120 h23 +0x200, Test of script
Gui Font

Gui Show, w481 h253, Window
Return


 
ScripStarts:
; ========== START OF SCRIPT ==========

 

; Get WMI service object.
winmgmts := ComObjGet("winmgmts:")

; Create sink objects for receiving event noficiations.
ComObjConnect(createSink := ComObjCreate("WbemScripting.SWbemSink"), "ProcessCreate_")
ComObjConnect(deleteSink := ComObjCreate("WbemScripting.SWbemSink"), "ProcessDelete_")

; Set event polling interval, in seconds.
interval := 2

; Register for process creation notifications:
winmgmts.ExecNotificationQueryAsync(createSink
    , "Select * from __InstanceCreationEvent"
    . " within " interval
    . " where TargetInstance isa 'Win32_Process'")

; Register for process deletion notifications:
winmgmts.ExecNotificationQueryAsync(deleteSink
    , "Select * from __InstanceDeletionEvent"
    . " within " interval
    . " where TargetInstance isa 'Win32_Process'")

; Don't exit automatically. I commented it out because I want to be able to run it to start and stop
;#Persistent

; Called when a new process is detected:
ProcessCreate_OnObjectReady(obj) {
    proc := obj.TargetInstance
    TrayTip New Process Detected, % "
    (LTrim
        ID:`t" proc.ProcessID "
        Parent:`t" proc.ParentProcessID "
        Name:`t" proc.Name "
        Path:`t" proc.ExecutablePath "
        
        Command line (requires XP or later):
        
        " proc.CommandLine
)

}

; Called when a process terminates:
ProcessDelete_OnObjectReady(obj) {
    proc := obj.TargetInstance
    TrayTip Process Terminated, % "
    (LTrim
        ID:`t" proc.Handle "
        Name:`t" proc.Name
)

}
 
 return

; ========== END OF SCRIPT ==========

 

ScriptStop:
;what do i put here 
reload
return


GuiEscape:
GuiClose:
    ExitApp
 
Nellybird

Re: How to start or stop this script

19 Dec 2017, 07:03

boiler wrote:You should just use the Pause command associated with the buttons. Use Pause, On with the Start button and Pause, Off with the stop button. Or just have one button as a toggle with just Pause. You would need to then put Lexikos' script after the GUI stuff and before the return ending with a Pause command if you want its initial state to be "stopped". Then your button routines and hotkeys would be the only things after the return that ends the auto-execute section.
can you review and see what I am doing wrong, I tried to follow what you said but it wasnt that clear to me

Code: Select all

#SingleInstance Force
SetWorkingDir %A_ScriptDir%

Gui Add, Button, x134 y59 w80 h23 gScripStarts, Start 
Gui Add, Button, x136 y112 w80 h23 gScriptStop, Stop
Gui Font, cFuchsia, Segoe Script
Gui Add, Text, x96 y33 w120 h23 +0x200, Test of script
Gui Font

Gui Show, w481 h253, Window
Return


 
ScripStarts:
pause, off
return

ScriptStop:
pause, on
return

GuiEscape:
GuiClose:
    ExitApp
; ========== START OF SCRIPT ==========

 
pause, on
; Get WMI service object.
winmgmts := ComObjGet("winmgmts:")

; Create sink objects for receiving event noficiations.
ComObjConnect(createSink := ComObjCreate("WbemScripting.SWbemSink"), "ProcessCreate_")
ComObjConnect(deleteSink := ComObjCreate("WbemScripting.SWbemSink"), "ProcessDelete_")

; Set event polling interval, in seconds.
interval := 2

; Register for process creation notifications:
winmgmts.ExecNotificationQueryAsync(createSink
    , "Select * from __InstanceCreationEvent"
    . " within " interval
    . " where TargetInstance isa 'Win32_Process'")

; Register for process deletion notifications:
winmgmts.ExecNotificationQueryAsync(deleteSink
    , "Select * from __InstanceDeletionEvent"
    . " within " interval
    . " where TargetInstance isa 'Win32_Process'")

; Don't exit automatically. I commented it out because I want to be able to run it to start and stop
;#Persistent

; Called when a new process is detected:
ProcessCreate_OnObjectReady(obj) {
    proc := obj.TargetInstance
    TrayTip New Process Detected, % "
    (LTrim
        ID:`t" proc.ProcessID "
        Parent:`t" proc.ParentProcessID "
        Name:`t" proc.Name "
        Path:`t" proc.ExecutablePath "
        
        Command line (requires XP or later):
        
        " proc.CommandLine
)

}

; Called when a process terminates:
ProcessDelete_OnObjectReady(obj) {
    proc := obj.TargetInstance
    TrayTip Process Terminated, % "
    (LTrim
        ID:`t" proc.Handle "
        Name:`t" proc.Name
)

}
 
 

; ========== END OF SCRIPT ==========

 

return

 
Nellybird

Re: How to start or stop this script

19 Dec 2017, 07:14

Rindis wrote:extremely quick and dirty:
Compared the the other option I was considering to get this thing to stop, your solution is not that dirty, although I won't use it.

Code: Select all

ScriptStop:
run, http://www.MyElectricCompany.com/TurnOffService
sleep, 1000
run, http://www.MyElectricCompany.com/RestartService
sleep, 1000
return
Obviously this is not a good solution if I am using a battery powered laptop or if I have a generator that kicks in when the electricity turns off, but hey, I cant spend another 5 days trying to figure out how to stop a script without breaking my computer in half.
User avatar
boiler
Posts: 12991
Joined: 21 Dec 2014, 02:44

Re: How to start or stop this script

19 Dec 2017, 09:23

Nellybird wrote:can you review and see what I am doing wrong, I tried to follow what you said but it wasnt that clear to me
boiler wrote:You would need to then put Lexikos' script after the GUI stuff and before the return ending with a Pause command if you want its initial state to be "stopped".
Like this:

Code: Select all

#SingleInstance Force
SetWorkingDir %A_ScriptDir%

Gui Add, Button, x134 y59 w80 h23 gScripStarts, Start 
Gui Add, Button, x136 y112 w80 h23 gScriptStop, Stop
Gui Font, cFuchsia, Segoe Script
Gui Add, Text, x96 y33 w120 h23 +0x200, Test of script
Gui Font

Gui Show, w481 h253, Window

; Get WMI service object.
winmgmts := ComObjGet("winmgmts:")

; Create sink objects for receiving event noficiations.
ComObjConnect(createSink := ComObjCreate("WbemScripting.SWbemSink"), "ProcessCreate_")
ComObjConnect(deleteSink := ComObjCreate("WbemScripting.SWbemSink"), "ProcessDelete_")

; Set event polling interval, in seconds.
interval := 2

; Register for process creation notifications:
winmgmts.ExecNotificationQueryAsync(createSink
    , "Select * from __InstanceCreationEvent"
    . " within " interval
    . " where TargetInstance isa 'Win32_Process'")

; Register for process deletion notifications:
winmgmts.ExecNotificationQueryAsync(deleteSink
    , "Select * from __InstanceDeletionEvent"
    . " within " interval
    . " where TargetInstance isa 'Win32_Process'")

; Don't exit automatically
#Persistent

; Called when a new process is detected:
ProcessCreate_OnObjectReady(obj) {
    proc := obj.TargetInstance
    TrayTip New Process Detected, % "
    (LTrim
        ID:`t" proc.ProcessID "
        Parent:`t" proc.ParentProcessID "
        Name:`t" proc.Name "
        Path:`t" proc.ExecutablePath "
        
        Command line (requires XP or later):
        
        " proc.CommandLine
)

}

; Called when a process terminates:
ProcessDelete_OnObjectReady(obj) {
    proc := obj.TargetInstance
    TrayTip Process Terminated, % "
    (LTrim
        ID:`t" proc.Handle "
        Name:`t" proc.Name
)

}

Pause, On
Return

ScripStarts:
pause, off
return

ScriptStop:
pause, on
return

GuiEscape:
GuiClose:
    ExitApp
I noticed that it seems to store up the events when it's stopped (paused) and report them when it's started again.
Guest

Re: How to start or stop this script

19 Dec 2017, 09:25

boiler wrote:You should just use the Pause command associated with the buttons. Use Pause, On with the Start button and Pause, Off with the stop button. Or just have one button as a toggle with just Pause. You would need to then put Lexikos' script after the GUI stuff and before the return ending with a Pause command if you want its initial state to be "stopped". Then your button routines and hotkeys would be the only things after the return that ends the auto-execute section.
I read your post again and made some progress. The one big issue left is, when I restart it (I hit start it runs ok, then stop and it pauses, ok) but when I start again it shows all the events that happened when the script was paused.

Code: Select all

#SingleInstance Force
SetWorkingDir %A_ScriptDir%

Gui Add, Button, x134 y59 w80 h23 gScripStarts, Start 
Gui Add, Button, x136 y112 w80 h23 gScriptStop, Stop
Gui Font, cFuchsia, Segoe Script
Gui Add, Text, x96 y33 w120 h23 +0x200, Test of script
Gui Font

Gui Show, w481 h253, Window
; ========== START OF SCRIPT ==========

pause, on
; Get WMI service object.
winmgmts := ComObjGet("winmgmts:")

; Create sink objects for receiving event noficiations.
ComObjConnect(createSink := ComObjCreate("WbemScripting.SWbemSink"), "ProcessCreate_")
ComObjConnect(deleteSink := ComObjCreate("WbemScripting.SWbemSink"), "ProcessDelete_")

; Set event polling interval, in seconds.
interval := 2

; Register for process creation notifications:
winmgmts.ExecNotificationQueryAsync(createSink
    , "Select * from __InstanceCreationEvent"
    . " within " interval
    . " where TargetInstance isa 'Win32_Process'")

; Register for process deletion notifications:
winmgmts.ExecNotificationQueryAsync(deleteSink
    , "Select * from __InstanceDeletionEvent"
    . " within " interval
    . " where TargetInstance isa 'Win32_Process'")

; Don't exit automatically. I commented it out because I want to be able to run it to start and stop
;#Persistent

; Called when a new process is detected:
ProcessCreate_OnObjectReady(obj) {
    proc := obj.TargetInstance
    TrayTip New Process Detected, % "
    (LTrim
        ID:`t" proc.ProcessID "
        Parent:`t" proc.ParentProcessID "
        Name:`t" proc.Name "
        Path:`t" proc.ExecutablePath "
        
        Command line (requires XP or later):
        
        " proc.CommandLine
)

}

; Called when a process terminates:
ProcessDelete_OnObjectReady(obj) {
    proc := obj.TargetInstance
    TrayTip Process Terminated, % "
    (LTrim
        ID:`t" proc.Handle "
        Name:`t" proc.Name
)

}
 
Return

ScripStarts:
pause, off
return


ScriptStop:
pause, on
return

GuiEscape:
GuiClose:
    ExitApp


return
Nellybird

Re: How to start or stop this script

19 Dec 2017, 09:36

I am also curious what makes this script keep on running even if there are no timers, loops, #persistent etc? Maybe if someone can solve that riddle I might actually be able to figure out (or someone else here) how to get this thing to work like a normal program that respects the word start and stop without having a mind on its own. It is frustrating when you worked on the same thing for so long. I tried looking for other tools that can alert for program starts and stops but so far no luck.

Anyone? Anything? :headwall:
User avatar
boiler
Posts: 12991
Joined: 21 Dec 2014, 02:44

Re: How to start or stop this script

19 Dec 2017, 09:52

Yes, as I mentioned in my post just above yours. That's apparently how his script works. You could try digging into it to see if it can be made not to queue up the events when paused, but using the quick and dirty method posted by Rindis would be a lot easier.
Nellybird

Re: How to start or stop this script

19 Dec 2017, 10:12

boiler wrote:Yes, as I mentioned in my post just above yours. That's apparently how his script works. You could try digging into it to see if it can be made not to queue up the events when paused, but using the quick and dirty method posted by Rindis would be a lot easier.
Thank u, I asked lexikos to come help if he can, this is a big mystery to me
User avatar
boiler
Posts: 12991
Joined: 21 Dec 2014, 02:44

Re: How to start or stop this script

19 Dec 2017, 11:03

Nellybird wrote:how to get this thing to work like a normal program that respects the word start and stop without having a mind on its own.
You can either use the quick and dirty method as suggested, or you could destroy the objects when you want to stop them, and recreate them when you want to start them.

Using the words "start" and "stop" in naming your routines has nothing to do with how a program works. No program would "respect" them. They are not AHK commands, functions, or keywords. And it is respecting the Pause command. And when it's unpaused, it's reporting what those objects have collected. It doesn't have a mind of its own, it's how those objects work. Doing the above is how you make it do what you want.
Nellybird

Re: How to start or stop this script

19 Dec 2017, 11:22

boiler wrote:
Nellybird wrote:how to get this thing to work like a normal program that respects the word start and stop without having a mind on its own.
You can either use the quick and dirty method as suggested, or you could destroy the objects when you want to stop them, and recreate them when you want to start them.

Using the words "start" and "stop" in naming your routines has nothing to do with how a program works. No program would "respect" them. They are not AHK commands, functions, or keywords. And it is respecting the Pause command. And when it's unpaused, it's reporting what those objects have collected. It doesn't have a mind of its own, it's how those objects work. Doing the above is how you make it do what you want.
You have a great forceful answer for pause. Now can you explain to me where in the holy texts of AHK does it explain how this keeps on running even without persistent, loop, timers etc? I think if you can share that wisdom maybe we can get back to dealing with the problem and not move into the realms of scripting language radicalism. I should have you know that I respect AHK, it's creator(s) and followers. I am just trying to learn. I do appreciate you teaching me the pause syntax.
User avatar
boiler
Posts: 12991
Joined: 21 Dec 2014, 02:44

Re: How to start or stop this script

19 Dec 2017, 11:31

He set up notifications, so once they are set up, it is an event that triggers things. Similar to a hotkey. You don't have to route your code to the hotkey routine to get it to execute; the event of someone pressing that key triggers it. That happens without persistent, loop, timers, etc.
Nellybird

Re: How to start or stop this script

19 Dec 2017, 11:53

boiler wrote:He set up notifications, so once they are set up, it is an event that triggers things. Similar to a hotkey. You don't have to route your code to the hotkey routine to get it to execute; the event of someone pressing that key triggers it. That happens without persistent, loop, timers, etc.
very good to know thank you! Can this be done without using notifications but instead place it in loop or timer and do the same?
User avatar
boiler
Posts: 12991
Joined: 21 Dec 2014, 02:44

Re: How to start or stop this script

19 Dec 2017, 12:06

No. It's driven by that event taking place.
Nellybird

Re: How to start or stop this script

19 Dec 2017, 12:19

boiler wrote:No. It's driven by that event taking place.
Can you suggest another method for me to research that can detect new and closed processes or either one?

Anything would help
User avatar
boiler
Posts: 12991
Joined: 21 Dec 2014, 02:44

Re: How to start or stop this script

19 Dec 2017, 12:39

Why not just use Rindis' method rather than trying to re-invent something that is pretty complex?
Nellybird

Re: How to start or stop this script

19 Dec 2017, 12:49

boiler wrote:Why not just use Rindis' method rather than trying to re-invent something that is pretty complex?
Because when it refreshes it removes the gui for a few seconds and i was trying to avoid that
BoBo
Posts: 6528
Joined: 13 May 2014, 17:15

Re: How to start or stop this script

19 Dec 2017, 13:01

@ Nellybird
they way you communicate you should consider registering an account, otherwise, each and every one of your postings need to be reviewed by a mod/admin before released - what might slow down your 'conversation'.

Return to “Ask For Help”

Who is online

Users browsing this forum: apilone, Flipeador, Google [Bot], Skrell, Smile_, twiz and 40 guests