running two functions at once

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
zayntheboss
Posts: 15
Joined: 24 Feb 2019, 15:21

running two functions at once

25 Feb 2019, 17:24

Hi!

So I cobbled together this function from others' work:

Code: Select all

#Persistent
SetTimer, Internet, 500
return

ConnectedToInternet(flag=0x40) { 
Return DllCall("Wininet.dll\InternetGetConnectedState", "Str", flag,"Int",0) 
}

Internet:
If ConnectedToInternet()
{
MsgBox Connected!
}
else {
MsgBox Not Connected :(
}
return

Esc::Exitapp
And it just periodically checks if there is an internet connection with settimer. Is there any way I can run this script continuously while running another function, and the instant there is no internet, pause the function, but when the connection is restored, restart the function? I only want to execute those commands once, though.
Portwolf
Posts: 161
Joined: 08 Oct 2018, 12:57

Re: running two functions at once

26 Feb 2019, 04:47

Might not be the cleanest aproarch, as i am a noob.. But:

Code: Select all

#Persistent
SetTimer, Internet, 500
return

ConnectedToInternet(flag=0x40) { 
	Return DllCall("Wininet.dll\InternetGetConnectedState", "Str", flag,"Int",0) 
	}

Internet:
If ConnectedToInternet()
	NetCheck := 1
	else
	NetCheck := 0

If NetCheck 1
	msgbox, Net up. Code it here to go on.
	else
	msgbox, Net down.

return
Last edited by Portwolf on 26 Feb 2019, 04:48, edited 1 time in total.
User avatar
labrint
Posts: 379
Joined: 14 Jun 2017, 05:06
Location: Malta

Re: running two functions at once

26 Feb 2019, 04:48

I don't think you can run two functions at once in a single script because AHK does not support multithreading.

What you can do is run this script in parallel with another script.

Code: Select all

#Persistent
SetTimer, Internet, 500
return

ConnectedToInternet(flag=0x40) { 
Return DllCall("Wininet.dll\InternetGetConnectedState", "Str", flag,"Int",0) 
}

Internet:
If ConnectedToInternet()
{
IniWrite, 1, InternetConnectivity.ini, InternetConnectivity, Key
}
else {
IniWrite, 0, InternetConnectivity.ini, InternetConnectivity, Key
}
return

Esc::Exitapp
The second script would use IniRead to check the value if it is 1 or 0 at several strategic intervals in the code where internet connectivity is a must.
Portwolf
Posts: 161
Joined: 08 Oct 2018, 12:57

Re: running two functions at once

26 Feb 2019, 05:16

From another topic:
HotKeyIt wrote:
10 Feb 2017, 05:14
AutoHotkey_H supports multi-threading using AutoHotkey.dll and AhkThread.
AutoHotkey_H v2 additionally supports multi-threading without AutoHotkey.dll, see ExeThread.
User avatar
evilC
Posts: 4823
Joined: 27 Feb 2014, 12:30

Re: running two functions at once

26 Feb 2019, 08:51

labrint wrote:
26 Feb 2019, 04:48
I don't think you can run two functions at once in a single script because AHK does not support multithreading.
This statement is partially true.
AHK emulates multi-threading.
Only one "thread" can be active at one time, but when you use something like SetTimer, what happens is, when the timer fires, it interrupts the current thread, runs the timer code, then relinquishes control back to the interrupted thread.
So as long as your SetTimer function ends (hits a return statement), then you have, in effect, multi-threaded.

The code the OP posted already does this, he just does not realize it.

Code: Select all

#Persistent
SetTimer, Internet, 500
Loop {
	Tooltip % "Other code running... " A_TickCount
	Sleep 100
}
return

ConnectedToInternet(flag=0x40) { 
Return DllCall("Wininet.dll\InternetGetConnectedState", "Str", flag,"Int",0) 
}

Internet:
If ConnectedToInternet()
{
	SoundBeep, 1000, 100
}
else {
	SoundBeep, 500, 100
}
return

Esc::Exitapp
zayntheboss
Posts: 15
Joined: 24 Feb 2019, 15:21

Re: running two functions at once

26 Feb 2019, 10:05

labrint wrote:
26 Feb 2019, 04:48
I don't think you can run two functions at once in a single script because AHK does not support multithreading.

What you can do is run this script in parallel with another script.

Code: Select all

#Persistent
SetTimer, Internet, 500
return

ConnectedToInternet(flag=0x40) { 
Return DllCall("Wininet.dll\InternetGetConnectedState", "Str", flag,"Int",0) 
}

Internet:
If ConnectedToInternet()
{
IniWrite, 1, InternetConnectivity.ini, InternetConnectivity, Key
}
else {
IniWrite, 0, InternetConnectivity.ini, InternetConnectivity, Key
}
return

Esc::Exitapp
The second script would use IniRead to check the value if it is 1 or 0 at several strategic intervals in the code where internet connectivity is a must.
WOW! This is what I was looking for. Even if this doesn't solve the problem (haven't checked yet) it's cool that you can store multiple pieces of information in a single file with the section and key! That's amazing, you could even store passwords for a login, products!!! Wow that's so cool

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 281 guests