Lua:createThreadSuspended

From Cheat Engine
Jump to navigation Jump to search

<> Lua API Reference

function createThreadSuspended(function(thread, ...), ... OPTIONAL) : Thread

Creates a thread for the given function, but does not start it immediately.

This function behaves like createNativeThread(?), except that the created thread remains suspended until resume() is called on the returned Thread object.

The thread function receives the Thread object as its first parameter, followed by any additional parameters passed to createThreadSuspended.

Function Declaration[edit]

1 function(thread, ...)

Function Parameters[edit]

Parameter Type Description
function Function The function to execute in the new thread. It receives the Thread object as its first argument.
... Any OPTIONAL Optional arguments that are passed to the thread function after the Thread object.

Returns[edit]

Thread — The created suspended Thread object.

Examples[edit]

Create a suspended thread[edit]

1 local thread = createThreadSuspended(function(thread)
2   print("This will not run until resume is called")
3 end)

Resume a suspended thread[edit]

1 local thread = createThreadSuspended(function(thread)
2   print("Thread started")
3 end)
4 
5 thread.resume()

Pass arguments to the suspended thread[edit]

 1 local message = "Hello from a suspended thread"
 2 local count = 3
 3 
 4 local thread = createThreadSuspended(function(thread, text, repetitions)
 5   for i = 1, repetitions do
 6     print(text .. " #" .. tostring(i))
 7   end
 8 end, message, count)
 9 
10 thread.resume()

Prepare a thread and start it later[edit]

1 local workerThread = createThreadSuspended(function(thread)
2   for i = 1, 5 do
3     print("Worker step " .. tostring(i))
4     sleep(500)
5   end
6 end)
7 
8 print("Thread created, but not running yet")
9 workerThread.resume()

Use synchronize after resuming[edit]

1 local thread = createThreadSuspended(function(thread)
2   local result = "Background work finished"
3 
4   synchronize(function()
5     print(result)
6   end)
7 end)
8 
9 thread.resume()

Create a suspended thread from a named function[edit]

1 local function worker(thread, message)
2   print(message)
3 end
4 
5 local message = "Running named worker"
6 
7 local thread = createThreadSuspended(worker, message)
8 
9 thread.resume()

Use the returned Thread object[edit]

1 local thread = createThreadSuspended(function(thread)
2   print("Thread object inside worker: " .. tostring(thread))
3 end)
4 
5 print("Returned thread object: " .. tostring(thread))

Resume conditionally[edit]

1 local thread = createThreadSuspended(function(thread)
2   print("Conditional start")
3 end)
4 
5 local shouldStart = true
6 
7 if shouldStart then
8   thread.resume()
9 end

Create multiple suspended threads[edit]

 1 local threads = {}
 2 
 3 for i = 1, 3 do
 4   threads[i] = createThreadSuspended(function(thread, index)
 5     print("Thread " .. tostring(index) .. " started")
 6   end, i)
 7 end
 8 
 9 print("All threads created")
10 
11 for i = 1, #threads do
12   threads[i].resume()
13 end

Main Pages

Core Lua documentation entry points

Lua
Script Engine

Thread Related Lua Functions

CPU information, process threads, main-thread execution, and queued callbacks