Lua:Class:Thread

From Cheat Engine
Jump to navigation Jump to search

{} Class

class Thread : Object

The Thread class represents a Lua thread object.

A Thread inherits from Object. It can execute a function in another system thread, start suspended, synchronize code with the main thread, and be asked to terminate.

Inheritance

Class Inherits From Description
Thread Object Executes Lua code in another system thread.

Creation

<> Lua API Reference

function createThread(function, ...) : Thread

<> Lua API Reference

function createThreadSuspended(function, ...) : Thread

<> Lua API Reference

function createThreadNewState(scripttext) : Thread

Creates a Thread object.

createThread executes the given function in another thread using the system thread mechanism. The function receives the Thread object as its first parameter, followed by any additional arguments.

createThreadSuspended creates a thread in a suspended state. It will not run until resume() is called.

createThreadNewState creates a new thread in a new Lua state. This is more efficient because no locking inside Lua takes place, but the thread has no access to user-defined Lua functions and only limited base Cheat Engine functions. The script is called inside a function(t), where t is the Thread object. Watch t.Terminated to know when to quit.

Unlike createThread, a thread created with createThreadNewState does not free itself on terminate by default, so the Result property can be read after the thread has finished.

Function Parameters

Function Parameter Description
createThread(function, ...) function The function to execute in another thread. The function receives the Thread object as the first parameter.
createThread(function, ...) ... Optional additional arguments passed to the thread function.
createThreadSuspended(function, ...) function The function to execute once the suspended thread is resumed. The function receives the Thread object as the first parameter.
createThreadSuspended(function, ...) ... Optional additional arguments passed to the thread function.
createThreadNewState(scripttext) scripttext The Lua script text to execute in a new Lua state.

Returns

Thread — The created Thread object.

Thread Function Signature

Function Signature Description
function(thread, ...) The thread function receives the Thread object as the first parameter, followed by any additional arguments passed to createThread or createThreadSuspended.

Properties

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

Methods

Method Return Type Description
freeOnTerminate(state) void When set to true, the Thread object frees itself when the function ends. The default is true. Use this only from inside the thread function, because the thread may already have terminated and freed itself when called from outside.
synchronize(function(thread, ...), ...) Any Called from inside the thread. Executes the given function in the main thread and waits for it to finish. Usually used for GUI access. Returns the return value of the given function.
waitfor() void Waits for the thread to finish. It is not recommended to call this from inside the thread itself.
suspend() void Suspends the thread's execution.
resume() void Resumes the thread's execution.
terminate() void Tells the thread it should terminate. The Terminated property becomes true.

Examples

local thread = createThread(function(thread)
  thread.Name = "Example Thread"

  for i = 1, 5 do
    if thread.Terminated then
      return
    end

    print("Thread step: " .. tostring(i))
    sleep(500)
  end
end)
local thread = createThread(function(thread, message, count)
  for i = 1, count do
    if thread.Terminated then
      return
    end

    print(message .. " " .. tostring(i))
    sleep(500)
  end
end, "Tick", 5)
local thread = createThreadSuspended(function(thread)
  print("This runs after resume()")
end)

thread.resume()
local form = createForm()
local label = createLabel(form)

label.Parent = form
label.Caption = "Waiting..."
form.show()

createThread(function(thread)
  sleep(1000)

  thread.synchronize(function(thread)
    label.Caption = "Updated from the main thread"
  end)
end)
local thread = createThread(function(thread)
  thread.freeOnTerminate(false)

  local count = 0

  while not thread.Terminated do
    count = count + 1
    sleep(500)
  end

  return tostring(count)
end)

sleep(2000)
thread.terminate()
thread.waitfor()

print("Thread result: " .. tostring(thread.Result))

thread.destroy()
local thread = createThreadNewState([[
  local count = 0

  while not t.Terminated do
    count = count + 1
    sleep(500)

    if count >= 5 then
      break
    end
  end

  return tostring(count)
]])

thread.waitfor()

print("Result: " .. tostring(thread.Result))

thread.destroy()

Main Pages

Core Lua documentation entry points

Lua
Script Engine