[Windows x64]WaitOnAddress
Posted: Tue Feb 14, 2023 11:51 am
See here for more information https://learn.microsoft.com/en-us/windo ... tonaddressThe WaitOnAddress function can be used by a thread to wait for a particular value to change from some undesired value to any other value. WaitOnAddress is more efficient than using the Sleep function inside a while loop because WaitOnAddress does not interfere with the thread scheduler. WaitOnAddress is also simpler to use than an event object because it is not necessary to create and initialize an event and then make sure it is synchronized correctly with the value. WaitOnAddress is not affected by low-memory conditions, other than potentially waking the thread early as noted below.
Any thread within the same process that changes the value at the address on which threads are waiting should call WakeByAddressSingle to wake a single waiting thread or WakeByAddressAll to wake all waiting threads. If WakeByAddressSingle is called, other waiting threads continue to wait.
Download the dll here https://1drv.ms/u/s!AufD7SJtv9Nuj5Uha2Y ... Q?e=y2YG3y
Code: Select all
OpenLibrary(0, "api-ms-win-core-synch-l1-2-0.dll")
Prototype WaitOnAddress(*Address, *CompareAddress, AddressSize.l, dwMilliseconds.l)
Prototype WakeByAddressSingle(*Address)
Global WaitOnAddress.WaitOnAddress = GetFunction(0, "WaitOnAddress")
Global WakeByAddressSingle.WakeByAddressSingle = GetFunction(0, "WakeByAddressSingle")
Global g_TargetValue
Procedure Thread(val)
Debug "Waiting for value at address " + @g_TargetValue + " to change"
UndesiredValue = 0 ;<-- if this value matches g_TargetValue, thread will sleep and wait for change
WaitOnAddress(@g_TargetValue, @UndesiredValue, 1, #INFINITE)
Debug "Value at address " + @g_TargetValue + " changed"
EndProcedure
Thread = CreateThread(@Thread(), 0)
g_TargetValue = 0 ;<-- same as Undesired value so thread will sleep
WakeByAddressSingle(@g_TargetValue)
Delay(3000)
g_TargetValue = 1 ;<-- different to Undesired value so thread will resume
WakeByAddressSingle(@g_TargetValue)
WaitThread(Thread)