Lua:Class:Thread
Jump to navigation
Jump to search
Thread class: (Inheritance: Object)
The thread class executes the given function in another thread using the systems thread mechanism
Creation[edit]
- createThread(function(Thread,...), ...)
- returns the Thread class object
Properties[edit]
- Name : string
- This name will be shown when the thread terminated abnormally
- Finished : boolean
- true if the thread has reached the end. Do not rely on this if the thread is freeOnTerminate(true) (which is the default)
- Terminated : function(timer)
- true if the Terminate method has been called
Methods[edit]
- freeOnTerminate(state) :
- When set to true the thread object will free itself when the function ends (default=true)
Note: Use this only from inside the thread function as the thread might have already terminated and freed itself when called
- synchronize(function(thread,...), ...)
- given function's return value
- Usually for GUI access. Called from inside the thread
- This will cause the thread to get the main thread to execute the given function and wait for it to finish.
- Returns the return value of the given function
- waitfor() :
- Waits for the given thread to finish (Not recommended to call this from inside the thread itself)
- suspend()
- Suspend the thread's execution
- resume() :
- Resume the thread;s executionmm
- terminate()
- Tells the thread it should terminate. The Terminated property will become true
Examples[edit]
local thread = createThread(function(timer) local i = 0 while i < 100 do i = i + 1 print(i) sleep(100) end return 50 end)
-- run given function after a delay local function delayed(func, delay) if type(func) ~= 'function' then return end if type(delay) ~= 'number' then delay = 1000 end local t = createTimer() t.Interval = delay t.OnTimer = function(t) -- only run once by destroying the timer object t.destroy() func() end end
delayed(function() print('suspending') thread.suspend() delayed(function() print('resuming') thread.resume() end, 3500) end, 5000)