<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.cheatengine.org/index.php?action=history&amp;feed=atom&amp;title=Lua%3AcreateThreadSuspended</id>
	<title>Lua:createThreadSuspended - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.cheatengine.org/index.php?action=history&amp;feed=atom&amp;title=Lua%3AcreateThreadSuspended"/>
	<link rel="alternate" type="text/html" href="https://wiki.cheatengine.org/index.php?title=Lua:createThreadSuspended&amp;action=history"/>
	<updated>2026-06-27T22:44:29Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.32.0</generator>
	<entry>
		<id>https://wiki.cheatengine.org/index.php?title=Lua:createThreadSuspended&amp;diff=8532&amp;oldid=prev</id>
		<title>Leunsel: Initial page creation.</title>
		<link rel="alternate" type="text/html" href="https://wiki.cheatengine.org/index.php?title=Lua:createThreadSuspended&amp;diff=8532&amp;oldid=prev"/>
		<updated>2026-06-27T01:08:58Z</updated>

		<summary type="html">&lt;p&gt;Initial page creation.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;[[Category:Lua]]&lt;br /&gt;
{{CodeBox|'''function''' createThreadSuspended(''function''(''thread'', ''...''), ''...'' OPTIONAL) ''':''' Thread}}&lt;br /&gt;
&lt;br /&gt;
Creates a thread for the given function, but does not start it immediately.&lt;br /&gt;
&lt;br /&gt;
This function behaves like [[Lua:createNativeThread|createNativeThread]](?), except that the created thread remains suspended until &amp;lt;code&amp;gt;resume()&amp;lt;/code&amp;gt; is called on the returned [[Lua:Class:Thread|Thread]] object.&lt;br /&gt;
&lt;br /&gt;
The thread function receives the Thread object as its first parameter, followed by any additional parameters passed to &amp;lt;code&amp;gt;createThreadSuspended&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===Function Declaration===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot; line highlight=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
function(thread, ...)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Function Parameters===&lt;br /&gt;
{|width=&amp;quot;85%&amp;quot; cellpadding=&amp;quot;10%&amp;quot; cellspacing=&amp;quot;0&amp;quot; border=&amp;quot;0&amp;quot;&lt;br /&gt;
!align=&amp;quot;left&amp;quot;|Parameter&lt;br /&gt;
!align=&amp;quot;left&amp;quot;|Type&lt;br /&gt;
!style=&amp;quot;width: 80%;background-color:white;&amp;quot; align=&amp;quot;left&amp;quot;|Description&lt;br /&gt;
|-&lt;br /&gt;
|function&lt;br /&gt;
|Function&lt;br /&gt;
|The function to execute in the new thread. It receives the Thread object as its first argument.&lt;br /&gt;
|-&lt;br /&gt;
|...&lt;br /&gt;
|Any OPTIONAL&lt;br /&gt;
|Optional arguments that are passed to the thread function after the Thread object.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Returns===&lt;br /&gt;
[[Lua:Class:Thread|Thread]] — The created suspended Thread object.&lt;br /&gt;
&lt;br /&gt;
===Examples===&lt;br /&gt;
&lt;br /&gt;
====Create a suspended thread====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot; line highlight=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
local thread = createThreadSuspended(function(thread)&lt;br /&gt;
  print(&amp;quot;This will not run until resume is called&amp;quot;)&lt;br /&gt;
end)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Resume a suspended thread====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot; line highlight=&amp;quot;1,5&amp;quot;&amp;gt;&lt;br /&gt;
local thread = createThreadSuspended(function(thread)&lt;br /&gt;
  print(&amp;quot;Thread started&amp;quot;)&lt;br /&gt;
end)&lt;br /&gt;
&lt;br /&gt;
thread.resume()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Pass arguments to the suspended thread====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot; line highlight=&amp;quot;4,10&amp;quot;&amp;gt;&lt;br /&gt;
local message = &amp;quot;Hello from a suspended thread&amp;quot;&lt;br /&gt;
local count = 3&lt;br /&gt;
&lt;br /&gt;
local thread = createThreadSuspended(function(thread, text, repetitions)&lt;br /&gt;
  for i = 1, repetitions do&lt;br /&gt;
    print(text .. &amp;quot; #&amp;quot; .. tostring(i))&lt;br /&gt;
  end&lt;br /&gt;
end, message, count)&lt;br /&gt;
&lt;br /&gt;
thread.resume()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Prepare a thread and start it later====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot; line highlight=&amp;quot;1,9&amp;quot;&amp;gt;&lt;br /&gt;
local workerThread = createThreadSuspended(function(thread)&lt;br /&gt;
  for i = 1, 5 do&lt;br /&gt;
    print(&amp;quot;Worker step &amp;quot; .. tostring(i))&lt;br /&gt;
    sleep(500)&lt;br /&gt;
  end&lt;br /&gt;
end)&lt;br /&gt;
&lt;br /&gt;
print(&amp;quot;Thread created, but not running yet&amp;quot;)&lt;br /&gt;
workerThread.resume()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use synchronize after resuming====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot; line highlight=&amp;quot;1,4,9&amp;quot;&amp;gt;&lt;br /&gt;
local thread = createThreadSuspended(function(thread)&lt;br /&gt;
  local result = &amp;quot;Background work finished&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  synchronize(function()&lt;br /&gt;
    print(result)&lt;br /&gt;
  end)&lt;br /&gt;
end)&lt;br /&gt;
&lt;br /&gt;
thread.resume()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Create a suspended thread from a named function====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot; line highlight=&amp;quot;7,9&amp;quot;&amp;gt;&lt;br /&gt;
local function worker(thread, message)&lt;br /&gt;
  print(message)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local message = &amp;quot;Running named worker&amp;quot;&lt;br /&gt;
&lt;br /&gt;
local thread = createThreadSuspended(worker, message)&lt;br /&gt;
&lt;br /&gt;
thread.resume()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Use the returned Thread object====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot; line highlight=&amp;quot;1,5&amp;quot;&amp;gt;&lt;br /&gt;
local thread = createThreadSuspended(function(thread)&lt;br /&gt;
  print(&amp;quot;Thread object inside worker: &amp;quot; .. tostring(thread))&lt;br /&gt;
end)&lt;br /&gt;
&lt;br /&gt;
print(&amp;quot;Returned thread object: &amp;quot; .. tostring(thread))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Resume conditionally====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot; line highlight=&amp;quot;1,8&amp;quot;&amp;gt;&lt;br /&gt;
local thread = createThreadSuspended(function(thread)&lt;br /&gt;
  print(&amp;quot;Conditional start&amp;quot;)&lt;br /&gt;
end)&lt;br /&gt;
&lt;br /&gt;
local shouldStart = true&lt;br /&gt;
&lt;br /&gt;
if shouldStart then&lt;br /&gt;
  thread.resume()&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Create multiple suspended threads====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot; line highlight=&amp;quot;4,12&amp;quot;&amp;gt;&lt;br /&gt;
local threads = {}&lt;br /&gt;
&lt;br /&gt;
for i = 1, 3 do&lt;br /&gt;
  threads[i] = createThreadSuspended(function(thread, index)&lt;br /&gt;
    print(&amp;quot;Thread &amp;quot; .. tostring(index) .. &amp;quot; started&amp;quot;)&lt;br /&gt;
  end, i)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
print(&amp;quot;All threads created&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
for i = 1, #threads do&lt;br /&gt;
  threads[i].resume()&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{LuaSeeAlso}}&lt;br /&gt;
&lt;br /&gt;
{{Threads}}&lt;/div&gt;</summary>
		<author><name>Leunsel</name></author>
		
	</entry>
</feed>