Page 1 of 1

Timer function with return parameter

Posted: Mon Jan 10, 2022 11:59 am
by S1N74X
Fist of all Happy new Year.

Currently iam fiddeling around with CEs Timer function
Spoiler

Code: Select all

{$lua}
if syntaxcheck then return end
[ENABLE]

local function countDown(count,text)
local timer = createTimer(getMainForm())
timer.Interval = 1000
timer.OnTimer = function(timer)
count = count - 1
if count < 1 then
timer.Destroy()
speakEnglish(text,true)
end
end
end

countDown(1,'1, runtime expired')
countDown(5,'5, runtime expired')
countDown(3,'3, runtime expired')

[DISABLE]
It works as expected.
My idea was to return a true or 1 after the timer expires.
But unfortunately that does not work.
Because i want to use multiple instances of the Timer, i dont want to use a gloabal value.
According to the Documentation it is possible to create Events, but i havent found any examples.

Any help would be appreciated.

Re: Timer function with return parameter

Posted: Mon Jan 10, 2022 12:56 pm
by ShyTwig16
Lua doesn't have native events, it's added by CE. And I have never needed to use the CE event class. But maybe someone else will chime in on that. But a simple solution would be to just use seperate threads and the sleep function. And then you won't be locking up the CE UI, but can still mostly work as usual. The only drawback is not all CE functions are thread safe, print is and many other things are. So depending on what you are doing you might have to synchronize some function calls.

Code: Select all

local function someThreadSafeFunc()
	return true
end
local function someFuncThatMustRunInMainThread(...)
	if not inMainThread() then
		error('Not in main thread.')
	else
		print(...)
	end
end
createThread(function(thread)
	print('1 Banana')
	sleep(1000)
	print('2 Banana')
	sleep(1000)
	print('3 Banana')
	sleep(1000)
	if someThreadSafeFunc() then
		print('Hooray!!')
	else
		print('Oh on!')
	end
	sleep(1000)
	synchronize(function(thread)
		someFuncThatMustRunInMainThread('Hooray main thread!')
	end)
	thread.terminate()
end)

Re: Timer function with return parameter

Posted: Mon Jan 10, 2022 1:41 pm
by S1N74X
ShyTwig16 wrote:
Mon Jan 10, 2022 12:56 pm
Lua doesn't have native events, it's added by CE. And I have never needed to use the CE event class. But maybe someone else will chime in on that. But a simple solution would be to just use seperate threads and the sleep function. And then you won't be locking up the CE UI, but can still mostly work as usual. The only drawback is not all CE functions are thread safe, print is and many other things are. So depending on what you are doing you might have to synchronize some function calls.

Code: Select all

local function someThreadSafeFunc()
	return true
end
local function someFuncThatMustRunInMainThread(...)
	if not inMainThread() then
		error('Not in main thread.')
	else
		print(...)
	end
end
createThread(function(thread)
	print('1 Banana')
	sleep(1000)
	print('2 Banana')
	sleep(1000)
	print('3 Banana')
	sleep(1000)
	if someThreadSafeFunc() then
		print('Hooray!!')
	else
		print('Oh on!')
	end
	sleep(1000)
	synchronize(function(thread)
		someFuncThatMustRunInMainThread('Hooray main thread!')
	end)
	thread.terminate()
end)
Thank you very much. :) +1
For my purpose i came up with this
Spoiler

Code: Select all

{$lua}
if syntaxcheck then return end
[ENABLE]

local function someThreadSafeFunc(count)
    while count >= 0 do
    print(count)
    sleep(1000)
    count = count - 1
    end
	return true
end

function doIt(count)
createThread(function(thread)
	if someThreadSafeFunc(count) then
		print('done with  : ',count)
		thread.terminate()
	else
		print('Oh no!')
	end
end)
end

doIt(10)
doIt(4)

[DISABLE]