Timer < 1.0 seconds ?
-
Wouldn't
sleep 0.2
suit your purpose? I don't know how precise it is. -
@dan rathbun said:
@thomthom said:
Anyone found a way to create a timer with resolution of less than a second?
So your saying that my 'wait half-second for console' technique, as shown in posting Re: idea: show console on script error, ie:
Sketchup.send_action(CMD_RUBY_CONSOLE) UI.start_timer(0.5, false) { puts e.message }
is really waiting a full second?No - it's waiting zero seconds. http://forums.sketchucation.com/viewtopic.php?f=180&t=25064&st=0&sk=t&sd=a&start=30#p216345
@unknownuser said:
Wouldn't sleep 0.2 suit your purpose? I don't know how precise it is.
No - because I don't want to pause all other execution.
-
I think I'm seeing 1/64th (0.016) sec resolution with Kernel.sleep (+/- 0.001) sec.
Is it possible to use sleep in a subthread, without pausing execution in the main thread?
(Note examples in the 'Pick-Axe' Ruby book, under Kernel.sleep and class Thread.)Here'e the test program I wrote this eve.
-
TT Watch Airshow!.
It implements the Animation interface, then calls
show_frame()
. Every hundred frames (about 24/sec.) it compares time elapsed to frames elapsed and adjust the delay up or down. Runs pretty close to 24 fps.All explained in Chapter 16, Professional Animation. You might enjoy the fairy tale.
-
@martinrinehart said:
TT Watch Airshow!.
It implements the Animation interface, then calls
show_frame()
. Every hundred frames (about 24/sec.) it compares time elapsed to frames elapsed and adjust the delay up or down. Runs pretty close to 24 fps.All explained in Chapter 16, Professional Animation. You might enjoy the fairy tale.
The thing is - I'm not making an animation. I'm just trying to run a piece of code 0.5 seconds later without blocking other code. (plus - I want to be able to cancel the timer under certain conditions before it triggers.)
-
@dan rathbun said:
Is it possible to use sleep in a subthread, without pausing execution in the main thread?
(Note examples in the 'Pick-Axe' Ruby book, under Kernel.sleep and class Thread.)It is possible, but very innacurate, because ruby threads only run while the ruby interpreter is running(commands are entered in the console, WebDialog is shown, Ruby Tool, etc.)
So one command might take half a second one time, and the next time take 2 seconds.
EDIT: I'm sure you know this already, but just putting it out in the open: Ruby threads don't act like native threads. If you are doing a lot in one thread, it'll slow down the other, and vice versa. See this:
http://en.wikipedia.org/wiki/Green_threads -
@thomthom said:
I'm just trying to run a piece of code 0.5 seconds later without blocking other code. (plus - I want to be able to cancel the timer under certain conditions before it triggers.)
Same, Same, for me.
It's kinda butt-backwards to what timeout.rb(from the standard library) does.
The timeout block method begins execution immediately, concurrent with it's timer. If the block terminates before the timeout period (it's timer runs out,) then the method returns true. If the timeout is reached before the block code finishes, a TimeoutError exception is raised.
-
Can to somebody it is useful
def at_timer(seconds, repeat=false, &block) Thread.new do Kernel.sleep seconds yield redo if repeat end end#def
-
I see 3 issues with Alex's example:
(1) There is no easy way to pass values (args) into the Thread block.
(2) The Thread block delay is repeated on each iteration instead of only before the first. Perhaps a means of doing either via a parameter to the outer method?
(3) Will the Thread object should be disposed of when the outer method ends, or is a call to Thread.kill needed (and therefore a reference name for the internal Thread object would also be needed.)
-
I'm pretty sure a thread "kills" itself after all the code is executed (not sure of the correct terminology).
By the way, what does & do in front of a variable (&block)?
-
@cjthompson said:
I'm pretty sure a thread "kills" itself after all the code is executed (not sure of the correct terminology).
By the way, what does & do in front of a variable (&block)?
turns the block you pass to a method into a Proc variable.
@unknownuser said:
def foobar(&block)
block.call
endfoobar {
puts 'Hello World'
} -
@dan rathbun said:
Perhaps something like this: ( NOT TESTED )
> .....sub.kill >
It is impossible to cause the method kill!
Otherwise tnread-object will halt existence instantly, and possibly block-code is not executed.
The kill method can be called only after the block-code is completed!
If a block-code is executed and variable repeat is a false, it is not necessary to cause thread.kill because of the thread-object has been dead at this moment. -
UPDATED: Perhaps something like this: ( NOT TESTED )
def at_timer(seconds, repeat=false, multidelay=false, *args, &block) result=nil; delay=true sub = Thread.new(args) do |args| Kernel.sleep(seconds) if delay delay=false unless multidelay result = yield(*args) redo if repeat end return result end#def
Does the args passing work OK?
Note:
(1) also I set it up so any kind of result could be returned from the yield block.
(2) it does NOT have any check on block_given?, should an exception be raised if no block is given ?? -
Perhaps something like this: ( UPDATED, see new codeblock later in topic. )
-
I haven't had time to test this code yet..Has anyone else had a chance?
Advertisement