sketchucation logo sketchucation
    • Login
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    Timer < 1.0 seconds ?

    Scheduled Pinned Locked Moved Developers' Forum
    17 Posts 6 Posters 1.4k Views 6 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • C Offline
      cjthompson
      last edited by

      @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

      1 Reply Last reply Reply Quote 0
      • Dan RathbunD Offline
        Dan Rathbun
        last edited by

        @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.

        I'm not here much anymore.

        1 Reply Last reply Reply Quote 0
        • A Offline
          AlexMozg
          last edited by

          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
          
          

          πŸ˜„

          1 Reply Last reply Reply Quote 0
          • Dan RathbunD Offline
            Dan Rathbun
            last edited by

            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 not here much anymore.

            1 Reply Last reply Reply Quote 0
            • C Offline
              cjthompson
              last edited by

              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)?

              1 Reply Last reply Reply Quote 0
              • thomthomT Offline
                thomthom
                last edited by

                @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
                end

                foobar {
                puts 'Hello World'
                }

                Thomas Thomassen β€” SketchUp Monkey & Coding addict
                List of my plugins and link to the CookieWare fund

                1 Reply Last reply Reply Quote 0
                • A Offline
                  AlexMozg
                  last edited by

                  @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.

                  1 Reply Last reply Reply Quote 0
                  • Dan RathbunD Offline
                    Dan Rathbun
                    last edited by

                    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 ??

                    I'm not here much anymore.

                    1 Reply Last reply Reply Quote 0
                    • Dan RathbunD Offline
                      Dan Rathbun
                      last edited by

                      Perhaps something like this: ( UPDATED, see new codeblock later in topic. )

                      I'm not here much anymore.

                      1 Reply Last reply Reply Quote 0
                      • Dan RathbunD Offline
                        Dan Rathbun
                        last edited by

                        I haven't had time to test this code yet..Has anyone else had a chance?

                        I'm not here much anymore.

                        1 Reply Last reply Reply Quote 0
                        • 1 / 1
                        • First post
                          Last post
                        Buy SketchPlus
                        Buy SUbD
                        Buy WrapR
                        Buy eBook
                        Buy Modelur
                        Buy Vertex Tools
                        Buy SketchCuisine
                        Buy FormFonts

                        Advertisement