Difference between revisions of "Lua:synchronize"

From Cheat Engine
Jump to navigation Jump to search
(Created page with "Category:Lua '''function''' synchronize(''Function'', ...) ''':''' any Calls the given function from the main thread, passing any additional arguments. Returns the retu...")
 
m (Added related function template.)
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
 
[[Category:Lua]]
 
[[Category:Lua]]
'''function''' synchronize(''Function'', ...) ''':''' any
+
{{CodeBox|'''function''' synchronize(''function'', ''...'') ''':''' any}}
  
Calls the given function from the main thread, passing any additional arguments.
+
Calls the given function from the main thread.
Returns the return value(s) of the given function.
+
 
 +
Any additional arguments are passed to the function when it is called. The return value of the given function is returned by synchronize.
  
 
===Function Parameters===
 
===Function Parameters===
Line 11: Line 12:
 
!style="width: 80%;background-color:white;" align="left"|Description
 
!style="width: 80%;background-color:white;" align="left"|Description
 
|-
 
|-
 +
|function
 
|Function
 
|Function
|Function
+
|The function to call from the main thread.
|The function to execute in the main thread.
 
 
|-
 
|-
 
|...
 
|...
|Any
+
|Any (optional)
|Optional arguments to pass to the function.
+
|Additional arguments that are passed to the function.
 
|}
 
|}
  
 
===Returns===
 
===Returns===
The return value(s) of the given function.
+
any — The return value of the given function.
  
 
===Examples===
 
===Examples===
<pre>
+
<syntaxhighlight lang="lua" line>
 +
local result = synchronize(function()
 +
  return getMainForm().Caption
 +
end)
 +
 
 +
print(result)
 +
</syntaxhighlight>
 +
 
 +
<syntaxhighlight lang="lua" line>
 +
local function add(a, b)
 +
  return a + b
 +
end
 +
 
 +
local result = synchronize(add, 10, 20)
 +
 
 +
print(result)
 +
</syntaxhighlight>
 +
 
 +
<syntaxhighlight lang="lua" line>
 
function ShowError(message)
 
function ShowError(message)
 
     if not inMainThread() then
 
     if not inMainThread() then
Line 34: Line 53:
 
     messageDialog(message, mtError, mbOK)
 
     messageDialog(message, mtError, mbOK)
 
end
 
end
</pre>
 
  
== See also ==
+
ShowError("Some Error!")
* [[Lua:inMainThread|inMainThread]]
+
</syntaxhighlight>
* [[Lua:createThread|createThread]]
+
 
 +
{{LuaSeeAlso}}
 +
 
 +
{{Threads}}

Latest revision as of 20:54, 26 June 2026

<> Lua API Reference

function synchronize(function, ...) : any

Calls the given function from the main thread.

Any additional arguments are passed to the function when it is called. The return value of the given function is returned by synchronize.

Function Parameters[edit]

Parameter Type Description
function Function The function to call from the main thread.
... Any (optional) Additional arguments that are passed to the function.

Returns[edit]

any — The return value of the given function.

Examples[edit]

1 local result = synchronize(function()
2   return getMainForm().Caption
3 end)
4 
5 print(result)
1 local function add(a, b)
2   return a + b
3 end
4 
5 local result = synchronize(add, 10, 20)
6 
7 print(result)
 1 function ShowError(message)
 2     if not inMainThread() then
 3         synchronize(function()
 4             self:ShowError(message)
 5         end)
 6         return
 7     end
 8     messageDialog(message, mtError, mbOK)
 9 end
10 
11 ShowError("Some Error!")

Main Pages

Core Lua documentation entry points

Lua
Script Engine

Thread Related Lua Functions

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