Wait for function to finish before going to second

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
coder_chick
Posts: 235
Joined: 05 Nov 2015, 10:43

Wait for function to finish before going to second

22 Jun 2017, 11:36

I have a script with close to 20 functions in a script where I want them to run one, wait until it finished, and then run the nextr - rather run them one after the other without waiting. I was thinking something like this, but it still runs both at the same time. I can't see how to get it to work without implement a ton of loops or gosubs to jump back and forth. Seems kind of stupid, but maybe I am missing something. Help a girl out! Thx! :crazy: :crazy:

This is just an example below, but same concept.

Code: Select all

check_if_ready = 1

check_if_ready = "1"
{
blah1()
}

check_if_ready = "1"  ;shouldn't run until function 1 finished
{
blah2()
}
msgbox,After functions run

blah1()
{
global
check_if_ready = ""


Gui, 1: Add, Text,  , GUI1
Gui, 1: Add, button,  gButton1, Close
Gui, 1: Show, w479 h379, Untitled GUI
return

Button1:
Gui 1: destroy
check_if_ready = 1
return
}

blah2()
{
global
check_if_ready = ""


Gui, 2: Add, Text,  , GUI2
Gui, 2: Add, button,  gButton2, Close
Gui, 2: Show, w479 h379, Untitled GUI
return

Button2:
Gui 2: destroy
check_if_ready = 1
return
}
♥ ❤ ❥ coder_chick ♥ ❤ ❥
phaleth
Posts: 38
Joined: 13 Apr 2015, 03:49

Re: Wait for function to finish before going to second

22 Jun 2017, 11:49

Return used inside a function once reached terminates the function. Nothing bellow the Return line will ever get executed. Functions are running one after another, your functions just don't ever finish as whole.
User avatar
coder_chick
Posts: 235
Joined: 05 Nov 2015, 10:43

Re: Wait for function to finish before going to second

22 Jun 2017, 12:18

Maybe I should provide more context and maybe my problem is something else. Here is a sample of two functions from my script. They are used to pull info from the system. Now, on some computers I am finding that the pull hangs so the function hangs. Therefore, I set a timer in hopes of having the function only run for a little bit and if it doesn't finish, end the function and move on. Doesn't seem to be working for me in situations where the timer time is exceeded.

Code: Select all

wmic_Win32_Group()
wmic_Win32_GroupUser()

wmic_Win32_Group()
{
global

wmic_logname = SystemLog-Win32_Group.txt
SetTimer,StopPullingLogs_11, 10000 ; try to pull logs for at least 10 seconds and then stop
sleep,15000 ;added for testing timer - should force it to go to label
namespace=\root\CIMV2
Class=Win32_Group

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
vOutput := ""
For Item in ComObjGet("winmgmts:").ExecQuery("Select * from Win32_Group") {
    For Key in ComObjGet("winmgmts:\\.\root\CIMV2:Win32_Group").Properties_ {
        vOutput .= Key.Name . "=" . Item[Key.Name] . "`r`n"
    }
}
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;	

	FileAppend,%vOutput%`n,%wmic_logname%.txt
	
SetTimer,StopPullingLogs_11, Off	
return

StopPullingLogs_11:
FileAppend,%wmic_logname%`n, ERROR.txt
SetTimer,StopPullingLogs_11, Off
return
}

wmic_Win32_GroupUser()
{
global

wmic_logname = SystemLog-Win32_GroupUser.txt
SetTimer,StopPullingLogs_12, 15000 ; try to pull logs for at least 10 seconds and then stop
sleep,15000 ;added for testing timer  - should force it to go to label
namespace=\root\CIMV2
Class=Win32_GroupUser

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
vOutput := ""
For Item in ComObjGet("winmgmts:").ExecQuery("Select * from Win32_GroupUser") {
    For Key in ComObjGet("winmgmts:\\.\root\CIMV2:Win32_GroupUser").Properties_ {
        vOutput .= Key.Name . "=" . Item[Key.Name] . "`r`n"
    }
}
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;	

	FileAppend,%vOutput%`n,%wmic_logname%.txt
	
SetTimer,StopPullingLogs_12, Off	
return

StopPullingLogs_12:
FileAppend,%wmic_logname%`n, ERROR.txt
SetTimer,StopPullingLogs_12, Off
return
}
♥ ❤ ❥ coder_chick ♥ ❤ ❥
User avatar
coder_chick
Posts: 235
Joined: 05 Nov 2015, 10:43

Re: Wait for function to finish before going to second

22 Jun 2017, 13:10

I believe the label return is causing it to go back to the function where it is stuck. I would like the return in the label to end the function and move on to the next function.
♥ ❤ ❥ coder_chick ♥ ❤ ❥
robertcollier4
Posts: 33
Joined: 09 Apr 2016, 22:14

Re: Wait for function to finish before going to second

22 Jun 2017, 13:27

https://autohotkey.com/board/topic/1245 ... lying-one/ - Using one thread to kill an underlying one
Last edited by robertcollier4 on 22 Jun 2017, 14:50, edited 3 times in total.
phaleth
Posts: 38
Joined: 13 Apr 2015, 03:49

Re: Wait for function to finish before going to second

22 Jun 2017, 13:29

SetTimer within a function definition doesn't work the same way as outside of the function, cause again once the function gets to a Return line it just terminates, and so does the SetTimer contained within it.

Inside of a function use loop instead of SetTimer.
robertcollier4
Posts: 33
Joined: 09 Apr 2016, 22:14

Re: Wait for function to finish before going to second

22 Jun 2017, 13:40

Last edited by robertcollier4 on 22 Jun 2017, 14:50, edited 3 times in total.
robertcollier4
Posts: 33
Joined: 09 Apr 2016, 22:14

Re: Wait for function to finish before going to second

22 Jun 2017, 13:43

You have your base thread. And then SetTimer is running in a separate thread. So how about the SetTimer thread will communicate back to the base thread that it should stop via setting a variable called "boolStopPullingLogs_11". Then have the base thread keep checking over and over again if it should stop.

What about this? I added the variable "boolStopPullingLogs_11"

Code: Select all

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
vOutput := ""
For Item in ComObjGet("winmgmts:").ExecQuery("Select * from Win32_Group") {
    If(boolStopPullingLogs_11 = 1) {
        Return
    }
    For Key in ComObjGet("winmgmts:\\.\root\CIMV2:Win32_Group").Properties_ {
        If(boolStopPullingLogs_11 = 1) {
            Return
        }
        vOutput .= Key.Name . "=" . Item[Key.Name] . "`r`n"
    }
}
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;	

	FileAppend,%vOutput%`n,%wmic_logname%.txt
	
SetTimer,StopPullingLogs_11, Off	
return

StopPullingLogs_11:
FileAppend,%wmic_logname%`n, ERROR.txt
SetTimer,StopPullingLogs_11, Off
boolStopPullingLogs_11 := 1
Return
}
User avatar
coder_chick
Posts: 235
Joined: 05 Nov 2015, 10:43

Re: Wait for function to finish before going to second

22 Jun 2017, 13:57

phaleth wrote:SetTimer within a function definition doesn't work the same way as outside of the function, cause again once the function gets to a Return line it just terminates, and so does the SetTimer contained within it.

Inside of a function use loop instead of SetTimer.
If the code runs successfully within the timeframe, I don't care if it terminates once it gets to the return. I'm terminating the timer before the return anyways.
♥ ❤ ❥ coder_chick ♥ ❤ ❥
User avatar
coder_chick
Posts: 235
Joined: 05 Nov 2015, 10:43

Re: Wait for function to finish before going to second

22 Jun 2017, 14:04

I basically just need it to try to pull the log for the seconds set in the timer. If it get WMIC info, disable the timer and move on to the next function. If it does not get the wmic info due to the timer time passing, call it a loss, log it in the error file, and stop anything more in the current function and move on to the next function.
♥ ❤ ❥ coder_chick ♥ ❤ ❥
robertcollier4
Posts: 33
Joined: 09 Apr 2016, 22:14

Re: Wait for function to finish before going to second

22 Jun 2017, 14:13

You are unable to kill a thread from another thread when the two threads are in the same file. Your SetTimer thread cannot interact with the base thread directly. So either:
1) Keep checking if the max time has elapsed at the beginning of each iteration of your for loop (you can use A_Now and see if a certain amount of time has passed since saving A_Now)
OR
2) Run your functions inside of different AHK files as described here (https://autohotkey.com/board/topic/9400 ... /?p=592081) so that your function gets its own Process ID so you can use "ExitApp" (from within the file) or "Process, Close" (from outside the file).
robertcollier4
Posts: 33
Joined: 09 Apr 2016, 22:14

Re: Wait for function to finish before going to second

22 Jun 2017, 14:43

https://autohotkey.com/docs/commands/Run.htm - Run to get a processID of a separate file.

Code: Select all

;file1 - run_logs.ahk
#Persistent
Run, %A_AhkPath%\wmic_Win32_Group.ahk, %A_AhkPath%, ,pIDwmic_Win32_Group

SetTimer,StopPullingLogs_11, -10000 ; try to pull logs for at least 10 seconds and then stop
Return

StopPullingLogs_11:
Process, Close, %pIDwmic_Win32_Group%
Process, WaitClose, %pIDwmic_Win32_Group%
Return

Code: Select all

;file2 - wmic_Win32_Group.ahk
; contents of wmic_Win32_Group() function

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: macromint, peter_ahk, Spawnova, wineguy and 280 guests