Difference between revisions of "Lua:synchronize"
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('' | + | {{CodeBox|'''function''' synchronize(''function'', ''...'') ''':''' any}} |
| − | Calls the given function from the main thread | + | 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=== | ===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 | ||
| − | + | |The function to call from the main thread. | |
| − | |The function to | ||
|- | |- | ||
|... | |... | ||
| − | |Any | + | |Any (optional) |
| − | | | + | |Additional arguments that are passed to the function. |
|} | |} | ||
===Returns=== | ===Returns=== | ||
| − | The return value | + | any — The return value of the given function. |
===Examples=== | ===Examples=== | ||
| − | < | + | <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 | ||
| − | |||
| − | + | ShowError("Some Error!") | |
| − | + | </syntaxhighlight> | |
| − | + | ||
| + | {{LuaSeeAlso}} | ||
| + | |||
| + | {{Threads}} | ||
Latest revision as of 20:54, 26 June 2026
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!")