Milli-second timer?
-
I found out UI.start_timer has a one-second resolution, so is there some way to implement a milli-second timer in Ruby without pausing execution?
Threads, maybe?
-
Typing 0.1 seem to work... ...or is there some issues I'm not seeing?
-
start_timer will accept a value less than a second - say 0.5, but it returns immediately, and doesn't wait for the 0.5 seconds.
-
Ah, rounds down to an Integer? hmm...
-
Get the Time and remember it, as say 'time'.
Keep getting the Time in a 'while time < time+clicks' loop till 'time+clicks' exceeds expectations - or / milliseconds /seconds / minutes etc after initial 'time'... -
Yeah, I've found that my code like
` now = Time.now
do lots of code here
now2 = Time.now
puts now2-now`
That will return a time down to the 1/100 second on my system.
-
But with that allow other code to run?
-
This is somewhat related, but I just found timeout.rb in the Ruby standard library.
With it, you can do this (abort a long-running operation) I have not tried too hard to break it, but it appears to work well. SketchUp still 'white-screens', but it does abort after the set time (20 seconds in this example)
begin timeout(20) do Sketchup.active_model.start_operation "Draw Sphere" up = Geom.linear_combination(0.5, Z_AXIS, 0.5, Y_AXIS) entities.add_group.entities.add_sphere([500, 500, 0], 200, up, 24) Sketchup.active_model.commit_operation end rescue TimeoutError Sketchup.active_model.abort_operation UI.messagebox("Operation Aborted - taking too long.") end
-
Yeah, what wanted to do is detect a "double-click" in a method called from a shortcut. Single tap - method a gets called, double tap - method b gets called.
I can do it using Time.now - last_click_time, but method a always gets fired under both circumstances. I'm fairly sure I need a millisecond timer to delay the first call to method a in case a double-tap occurs.
Like maybe the normal single-tap runs the method, but a double-tap brings up a config dialog.
-
@jim said:
With it, you can do this (abort a long-running operation)
Oh yeah, I'm definitely going to try to tie this in to start_operation. That way, I can set a single timeout limit for all scripts transparently.
Advertisement