Page 1 of 1

send in time?

Posted: Wed Apr 10, 2024 8:51 am
by Zax
Hello,

Just to be sure, do the following scripts do the exact same things, at the same time?

Code: Select all

on myProcess_A
	send "myProcess_B" to me in 100 milliseconds
end myProcess_A

on myProcess_B
	put the date into fld 1
end myProcess_B

Code: Select all

on myProcess
	wait 100 milliseconds with messages
	put the date into fld 1
end myProcess

Re: send in time?

Posted: Wed Apr 10, 2024 10:01 am
by LCMark
@Zax:

Assuming nothing else is going on (i.e. no messages are sent during the wait) then yes they will do the same thing.

However, you will not see quite the same result if a message is handled in that 100ms window which also calls wait:

This code will cause 'the date' to populate field 1 after 1 second - not 100 milliseconds.

Code: Select all

on myProcess
        send "imitateWaitingButtonPress" to me in 50 milliseconds
	wait 100 milliseconds with messages
	put the date into fld 1
end myProcess

on imitateWaitingButtonPress
    wait 1s with messages
end imitateWaitingButtonPress
This code will cause 'the date' to populate field 1 after 100 milliseconds:

Code: Select all

on myProcess_A
        send "imitateWaitingButtonPress" to me in 50 milliseconds
   
	send "myProcess_B" to me in 100 milliseconds
end myProcess_A

on myProcess_B
	put the date into fld 1
end myProcess_B

on imitateWaitingButtonPress
    wait 1s with messages
end imitateWaitingButtonPress
The reason is that waiting is recursive.

In the myProcess handler the 'wait' has to finish before the 'put' will execute - and wait only finishes when all handlers which have been called while the wait is happening (due to messages being handled) have finished.

In the myProcess_A handler - there is no (recursive) wait - so myProcess_A finishes as soon as the second send is done. Then the imitateWaitingButtonPress message will fire, and it will wait - but because that wait is with messages the myProcess_B pending message will still be handled at the expected time - just 'inside' the wait 1s.

Re: send in time?

Posted: Wed Apr 10, 2024 10:49 am
by Zax
Thank you LCMark for this precise and complete response.
In the specific case of launching a standalone, in which I have to wait for large substacks to load, what kind of script do you recommend?

---------------------------- kind A

Code: Select all

on preOpenStack
	insert the script of stack "CustomErrorReport" into front
	insert the script of stack "MyLib" into back
	start using stack "BigImagesLib"
	
	send "preOpenStack_readPrefs" to me in 100 milliseconds // to be sure libs will be loaded
end preOpenStack

on preOpenStack_readPrefs
	... read a prefs file if any
end preOpenStack_readPrefs

---------------------------- kind B

Code: Select all

on preOpenStack
	insert the script of stack "CustomErrorReport" into front
	insert the script of stack "MyLib" into back
	start using stack "BigImagesLib"
	
	wait 100 milliseconds with messages // to be sure libs will be loaded
	
	... read a prefs file if any
end preOpenStack