Difference between revisions of "Lua:Class:Thread"
m |
(Major overhaul of the post.) |
||
| (One intermediate revision by the same user not shown) | |||
| Line 1: | Line 1: | ||
[[Category:Lua]] | [[Category:Lua]] | ||
| − | + | {{Class|'''class''' Thread ''':''' Object}} | |
| − | The Thread class represents a | + | 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=== | |
| − | + | {|width="85%" cellpadding="10%" cellspacing="0" border="0" | |
| + | !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. | ||
| + | |} | ||
| + | |||
| + | ===Creation=== | ||
| + | {{CodeBox|'''function''' createThread(''function'', ''...'') ''':''' Thread}} | ||
| + | {{CodeBox|'''function''' createThreadSuspended(''function'', ''...'') ''':''' Thread}} | ||
| + | {{CodeBox|'''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=== | ||
| + | {|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. | ||
| + | |} | ||
| + | |||
| + | ===Returns=== | ||
| + | Thread — The created Thread object. | ||
| − | + | ===Thread Function Signature=== | |
| − | + | {|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. | ||
| + | |} | ||
| − | == Properties == | + | ===Properties=== |
| − | {| | + | {|width="85%" cellpadding="10%" cellspacing="0" border="0" |
| − | ! Property | + | !align="left"|Property |
| − | ! Type | + | !align="left"|Type |
| − | ! Description | + | !style="width: 80%;background-color:white;" align="left"|Description |
|- | |- | ||
| − | | Name | + | |Name |
| − | | String | + | |String |
| − | | The name | + | |The thread name. This name is shown when the thread terminates abnormally. |
|- | |- | ||
| − | | Finished | + | |Finished |
| − | | Boolean | + | |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 | + | |Terminated |
| − | | Boolean | + | |Boolean |
| − | | | + | |Returns true if terminate() has been called. |
|- | |- | ||
| − | | Result | + | |Result |
| − | | String | + | |String |
| − | | The result of the thread function as a string. | + | |The result of the thread function as a string. |
|} | |} | ||
| − | == Methods == | + | ===Methods=== |
| − | {| | + | {|width="85%" cellpadding="10%" cellspacing="0" border="0" |
| − | ! Method | + | !align="left"|Method |
| − | ! | + | !align="left"|Return Type |
| − | + | !style="width: 80%;background-color:white;" align="left"|Description | |
| − | ! Description | ||
|- | |- | ||
| − | | freeOnTerminate | + | |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. |
| − | | When set to true, the | ||
| − | |||
|- | |- | ||
| − | | synchronize | + | |synchronize(function(thread, ...), ...) |
| − | + | |Any | |
| − | | 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. |
| − | | Called from inside the thread. | ||
| − | Usually used for GUI access. Returns the return value of the function. | ||
|- | |- | ||
| − | | waitfor | + | |waitfor() |
| − | | | + | |void |
| − | + | |Waits for the thread to finish. It is not recommended to call this from inside the thread itself. | |
| − | | Waits for the thread to finish. | ||
|- | |- | ||
| − | | suspend | + | |suspend() |
| − | | | + | |void |
| − | + | |Suspends the thread's execution. | |
| − | | Suspends the thread's execution. | ||
|- | |- | ||
| − | | resume | + | |resume() |
| − | | | + | |void |
| − | + | |Resumes the thread's execution. | |
| − | | Resumes the thread's execution. | ||
|- | |- | ||
| − | | terminate | + | |terminate() |
| − | | | + | |void |
| − | + | |Tells the thread it should terminate. The Terminated property becomes true. | |
| − | | Tells the thread it should terminate. The | ||
|} | |} | ||
| − | == Examples == | + | ===Examples=== |
| + | <pre> | ||
| + | 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) | ||
| + | </pre> | ||
| + | |||
<pre> | <pre> | ||
| − | + | local thread = createThread(function(thread, message, count) | |
| − | local | + | for i = 1, count do |
| − | local | + | 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) | ||
| + | </pre> | ||
| + | |||
| + | <pre> | ||
| + | local thread = createThread(function(thread) | ||
| + | thread.freeOnTerminate(false) | ||
| + | |||
| + | local count = 0 | ||
| − | |||
| − | |||
| − | |||
| − | |||
while not thread.Terminated do | while not thread.Terminated do | ||
| − | sleep(500) | + | count = count + 1 |
| + | sleep(500) | ||
end | end | ||
| − | |||
| − | |||
| − | + | return tostring(count) | |
| − | + | end) | |
| − | |||
| − | |||
| − | + | sleep(2000) | |
| − | + | thread.terminate() | |
| − | + | thread.waitfor() | |
| − | + | ||
| − | + | print("Thread result: " .. tostring(thread.Result)) | |
| − | if | + | |
| − | + | thread.destroy() | |
| + | </pre> | ||
| + | |||
| + | <pre> | ||
| + | local thread = createThreadNewState([[ | ||
| + | local count = 0 | ||
| + | |||
| + | while not t.Terminated do | ||
| + | count = count + 1 | ||
| + | sleep(500) | ||
| + | |||
| + | if count >= 5 then | ||
| + | break | ||
end | end | ||
end | end | ||
| − | |||
| − | |||
| − | + | return tostring(count) | |
| − | + | ]]) | |
| − | + | ||
| − | + | thread.waitfor() | |
| − | + | ||
| − | + | print("Result: " .. tostring(thread.Result)) | |
| − | + | ||
| − | + | thread.destroy() | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
</pre> | </pre> | ||
{{LuaSeeAlso}} | {{LuaSeeAlso}} | ||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
Latest revision as of 19:46, 23 June 2026
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.
Contents
Inheritance[edit]
| Class | Inherits From | Description |
|---|---|---|
| Thread | Object | Executes Lua code in another system thread. |
Creation[edit]
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()