CElua createThread(..)

Using sleep(*) function without freezing CE GUI

Place snippets of code and scripts under here to share with others. Archives and .Lua extensions are allowed


Moderator: Table Moderator

Post Reply
User avatar
J1327
Donor
Donor
Novice Hacker
Novice Hacker
Posts: 43
Joined: Mon Jul 25, 2022 5:00 pm
Answers: 0
Location: Baltic States
x 48

CElua createThread(..)

Post by J1327 »

CElua createThread(..)

ryouma17 wrote: Wed Jul 05, 2023 11:52 am

at first thought "while" Lua code will help with writeInteger(address) and "sleep" - yet as soon as idea hit, it reminded me that using directly sleep function, CE GUI freezes...

Anyway,

Thankfully I found info about sleep and or os.sleep information from here https://www.cheatengine.org/forum/viewt ... p?t=620168 and https://www.cheatengine.org/forum/viewt ... p?t=608432
quote by @ParkourPenguin

Lua code is run in the main thread by default. Only the main thread is allowed to access the GUI. If you block the main thread (i.e., via sleep), the GUI stops working.

Hm. So I could use coroutine (is referenced in Lua docs as threads), yet since "sleep" function it-self is "C function" -- will cause an error " yield across a C-call boundary". Script Error. Which I tried and happened to me. So, I need an alternative...

A bit more browsing found createThread(...). Which means, allows sleep function to be used without freezing CE Gui.
note at inMainThread() -- checks if code in main Lua thread

Now in order to use sleep you can use following code (without freezing CE Gui) with createThread():

Code: Select all

function threadFunction(thread)
-- Perform actions in the thread
thread:terminate()
end

-- Create and start the thread
local thread = createThread(threadFunction)

Before you go to example code,
~ note while stepping with Lua debugger with in Lua code, you can't debug threadFunction.
~ you can only debug your Lua function via declaring directly "threadFunction()" which will make function run in main thread.
~ using threads make sure you will close threads them correctly! (Running with too many unproperly handled activate threads might run into issues, such as is with a timer’s “stack overflow”)

While createThread() in some "example" code :

Code: Select all

function threadFunction(thread)
local counter = 0
local hit = 24

while counter < hit do
                    print("Does code running inside of main thread -- "..tostring(inMainThread()))
                    counter = counter + 1
                    sleep(1000) -- won't freeze CE GUI.
                    terminationCondition = true;
                    end

if terminationCondition then
   print("Closing thread.")
   thread:terminate() -- Call the terminate method on the thread object
end

end -- end thread function


thread = createThread(threadFunction) -- call function with new thread.

🙃
Ṯ̸͋͠H̷̻́Ē̵̦̇R̴̛̠̞̾E̷̥̗̎̾ ̷̩̽̂Ì̵̱S̵̲͘ ̷̝̝̽͆S̵̯̒T̷̩͈̃͑Î̶̯̟Ļ̴̬̈L̴̘̱̏̒ ̵̨̟̈́͠T̵͓͎͐I̶̥͈̽M̷̡̛͒E̶͙͗


Post Reply