Lua:Class:Thread

From Cheat Engine
Revision as of 19:32, 11 July 2025 by Leunsel (talk | contribs)
Jump to navigation Jump to search

Thread class: (Inheritance: Object)

The Thread class represents a system thread in Cheat Engine, allowing you to execute Lua code concurrently. Threads can be created using createThread, createThreadSuspended, or createThreadNewState.

Creation Functions

  • createThread(function(Thread, ...), ...)
    • Executes the given function in a new thread. Returns a Thread object.
    • The function declaration should be: function(Thread, ...)
  • createThreadSuspended(function(Thread, ...), ...)
    • Same as createThread, but the thread will not run until resume() is called.
  • createThreadNewState(scripttext)
    • Creates a new thread in a new Lua state.
    • This is more efficient (no Lua locking), but has no access to user-defined Lua functions and only limited base CE functions.
    • The script is called inside function(t) where t is the thread object.
    • Watch for t.Terminated to quit.
    • Note: Unlike createThread, the created thread does not freeOnTerminate by default, so you can read the Result property after the thread finishes.

Properties

Property Type Description
Name String The name of the thread (shown if the thread terminates abnormally).
Finished Boolean True if the thread has reached the end. Do not rely on this if freeOnTerminate(true) is set (default is true).
Terminated Boolean True if the terminate() method has been called.
Result String The result of the thread function as a string.

Methods

Method Parameters Returns Description
freeOnTerminate Boolean (state) None When set to true, the thread object will free itself when the function ends (default = true).

Note: Use only from inside the thread function.

synchronize function(thread, ...), ... Any Called from inside the thread. Causes the main thread to execute the given function and waits for it to finish.

Usually used for GUI access. Returns the return value of the function.

waitfor None None Waits for the thread to finish. (Not recommended to call from inside the thread itself.)
suspend None None Suspends the thread's execution.
resume None None Resumes the thread's execution.
terminate None None Tells the thread it should terminate. The Terminated property will become true.

Examples

-- Table to hold thread objects
local threads = {}
local threadCount = 5

-- Function executed by each thread
local function threadFunc(thread, number)
  print("Thread " .. number .. " started")
  -- Loop until the thread is terminated
  while not thread.Terminated do
    sleep(500) -- Simulate work
  end
  print("Thread " .. number .. " terminated")
end

-- Create and start the threads
for i = 1, threadCount do
  threads[i] = createThread(function(thread) threadFunc(thread, i) end)
end

-- Timer to automatically terminate all threads after 5 seconds
local terminateTimer = createTimer()
terminateTimer.Interval = 5000
terminateTimer.OnTimer = function(timer)
  for i = 1, threadCount do
    if threads[i] and not threads[i].Terminated then
      threads[i]:terminate()
    end
  end
  timer.destroy() -- Stop the timer after terminating threads
end

-- Timer to terminate all threads if ESC is pressed
local escTimer = createTimer()
escTimer.Interval = 100
escTimer.OnTimer = function(timer)
  if isKeyPressed(VK_ESCAPE) then
    print("ESC pressed, terminating all threads.")
    for i = 1, threadCount do
      if threads[i] and not threads[i].Terminated then
        threads[i]:terminate()
      end
    end
    timer.destroy() -- Stop the timer after handling ESC
  end
end

See also

Related Functions