Difference between revisions of "Lua:Class:Thread"

From Cheat Engine
Jump to navigation Jump to search
(Methods)
(Major overhaul of the post.)
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
[[Category:Lua]]
 
[[Category:Lua]]
Thread '''class''': ('''Inheritance''': ''[[Lua:Class:Object|Object]]'')
+
{{Class|'''class''' Thread ''':''' Object}}
  
The thread class executes the given function in another thread using the systems thread mechanism
+
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.
  
== Creation ==
+
===Inheritance===
; [[Lua:createThread|createThread]](''function(Thread,...)'', ...)
+
{|width="85%" cellpadding="10%" cellspacing="0" border="0"
: returns the Thread class object
+
!align="left"|Class
 +
!align="left"|Inherits From
 +
!style="width: 80%;background-color:white;" align="left"|Description
 +
|-
 +
|Thread
 +
|Object
 +
|Executes Lua code in another system thread.
 +
|}
  
== Properties ==
+
===Creation===
; Name : string
+
{{CodeBox|'''function''' createThread(''function'', ''...'') ''':''' Thread}}
: This name will be shown when the thread terminated abnormally
+
{{CodeBox|'''function''' createThreadSuspended(''function'', ''...'') ''':''' Thread}}
 +
{{CodeBox|'''function''' createThreadNewState(''scripttext'') ''':''' Thread}}
  
; Finished : boolean
+
Creates a Thread object.
: ''true'' if the thread has reached the end. Do not rely on this if the thread is freeOnTerminate(true) (which is the default)
 
  
; Terminated : function(timer)
+
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.
: ''true'' if the Terminate method has been called
 
  
== Methods ==
+
createThreadSuspended creates a thread in a suspended state. It will not run until resume() is called.
  
; freeOnTerminate(''state'') :
+
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.
: When set to true the thread object will free itself when the function ends (default=true)
 
<blockquote>Note: Use this only from inside the thread function as the thread might have already terminated and freed itself when called</blockquote>
 
  
; synchronize(''function(thread,...)'', ...) : given function's return value
+
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.
: Usually for GUI access. Called from inside the thread
 
; This will cause the thread to get the main thread to execute the given function and wait for it to finish.
 
; Returns the return value of the given function
 
  
; waitfor() &#58;
+
===Function Parameters===
: Waits for the given thread to finish (Not recommended to call this from inside the thread itself)
+
{|width="85%" cellpadding="10%" cellspacing="0" border="0"
 +
!align="left"|Function
 +
!align="left"|Parameter
 +
!style="width: 80%;background-color:white;" align="left"|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.
 +
|}
  
; suspend()
+
===Returns===
: Suspend the thread's execution
+
Thread — The created Thread object.
  
; resume() &#58;
+
===Thread Function Signature===
: Resume the thread;s executionmm
+
{|width="85%" cellpadding="10%" cellspacing="0" border="0"
 +
!align="left"|Function Signature
 +
!style="width: 80%;background-color:white;" align="left"|Description
 +
|-
 +
|function(thread, ...)
 +
|The thread function receives the Thread object as the first parameter, followed by any additional arguments passed to createThread or createThreadSuspended.
 +
|}
  
; terminate()
+
===Properties===
: Tells the thread it should terminate. The Terminated property will become true
+
{|width="85%" cellpadding="10%" cellspacing="0" border="0"
 +
!align="left"|Property
 +
!align="left"|Type
 +
!style="width: 80%;background-color:white;" align="left"|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.
 +
|}
  
== Examples ==
+
===Methods===
  local thread = createThread(function(timer)
+
{|width="85%" cellpadding="10%" cellspacing="0" border="0"
    local i = 0
+
!align="left"|Method
    while i < 100 do
+
!align="left"|Return Type
      i = i + 1
+
!style="width: 80%;background-color:white;" align="left"|Description
      print(i)
+
|-
       sleep(100)
+
|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===
 +
<pre>
 +
local thread = createThread(function(thread)
 +
  thread.Name = "Example Thread"
 +
 
 +
  for i = 1, 5 do
 +
    if thread.Terminated then
 +
       return
 
     end
 
     end
     return 50
+
 
 +
     print("Thread step: " .. tostring(i))
 +
    sleep(500)
 +
  end
 +
end)
 +
</pre>
 +
 
 +
<pre>
 +
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)
 +
</pre>
 +
 
 +
<pre>
 +
local thread = createThreadSuspended(function(thread)
 +
  print("This runs after resume()")
 +
end)
 +
 
 +
thread.resume()
 +
</pre>
 +
 
 +
<pre>
 +
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)
 +
end)
 +
</pre>
 +
 +
<pre>
 +
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()
 +
</pre>
 +
 +
<pre>
 +
local thread = createThreadNewState([[
 +
  local count = 0
 +
 +
  while not t.Terminated do
 +
    count = count + 1
 +
    sleep(500)
  
  -- run given function after a delay
+
     if count >= 5 then
  local function delayed(func, delay)
+
       break
     if type(func) ~= 'function' then return end
 
    if type(delay) ~= 'number' then delay = 1000 end
 
    local t = createTimer()
 
    t.Interval = delay
 
    t.OnTimer = function(t)
 
      -- only run once by destroying the timer object
 
      t.destroy()
 
       func()
 
 
     end
 
     end
 
   end
 
   end
  
   delayed(function()
+
   return tostring(count)
    print('suspending')
+
]])
    thread.suspend()
+
 
    delayed(function()
+
thread.waitfor()
      print('resuming')
+
 
      thread.resume()
+
print("Result: " .. tostring(thread.Result))
    end, 3500)
+
 
  end, 5000)
+
thread.destroy()
 +
</pre>
  
 
{{LuaSeeAlso}}
 
{{LuaSeeAlso}}
 
=== Related Functions ===
 
* [[Lua:createThreadSuspended|createThreadSuspended]]
 

Latest revision as of 19:46, 23 June 2026

{} 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[edit]

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

Creation[edit]

<> 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[edit]

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[edit]

Thread — The created Thread object.

Thread Function Signature[edit]

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[edit]

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[edit]

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[edit]

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