2 Replies Latest reply on Jul 2, 2013 2:24 AM by Gerbrand Stap

    Issue with WebView.execute_js in threads

    Mark Bauer

      I am attempting to call WebView.execute_js within a call back method in a thread, and the thread immediately dies.  The interesting part is that I am able to call javascript alerts, but cannot call any other javascript functions that I wrote.  Please let me know of any ideas, or if you have had any similar experiences.  Below is some sample code that is very similar to what I am trying to do. 

       

      In separate .rb :

        def listen( callback)

           sp = SerialPort.new(comm_port_name, baud_rate, data_bits, stop_bits, get_parity_const(parity_key))#get_parity_const gets the appropriate constant based on a key

          #always be getting the next character

          sp.read_timeout = -1

          serial_out = ''

              t = Thread.new {

                begin

                  char = ''

                  char = sp.getc

                  if !char.nil?

                    #puts comm_port_name+' - CHAR:['+char+']'

                    serial_out = serial_out+char

                    if char.unpack('H*').join == "0d"

                      callback.call(serial_out.strip)

                      serial_out = ''

                    end

                  else

        #           If more speed is needed, we can adjust the sleep time, the only reason to sleep is to reduce load on processor

                    sleep(0.1)

                  end

                rescue => e

                  callback.call(comm_port_name,"Error, stopping comm port listener ERROR: #{e.inspect}")

                  Thread.exit

                end while true

                Thread.exit

              }

        end

       

       

      In controller:

        def b_callback()

          begin

            WebView.execute_js("alert('In callback!');") #This works

            WebView.execute_js("addToLog('1');") #This does not and kills the thread

          end

        end

       

       

      Thanks,

       

      MAB

        • Re: Issue with WebView.execute_js in threads
          Mark Bauer

          So in continuing to look into this issue, I have discovered it is a ruby problem and doesn't have to do with WebView.execute_js at all.  If anyone can take a look and tell me what I am doing incorrectly, or how I could accomplish the same thing (a threaded callback that can manipulate an object) a better way please let me know.  Below is a simple program that replicates the issue: 

           

          In phone.rb:

          class Phone

                      @@ringer = "RingVibrateRingVibrateRing"

                      def self.get_ringer

                                  return @@ringer

                      end

           

                      def set_ringer(new_ring)

                                  @@ringer = new_ring

                      end

           

                      def self.calling_you(reached)

                                  puts "Calling you.  "

                                  t = Thread.new {

                                              sleep(3)

                                              reached.call

                                              Thread.exit

                                  }

                                  sleep(10)

                                  puts "Thanks for calling me back!"

                      end

          end

           

           

          In CallTree.rb

          require 'thread'

          require 'phone.rb'

           

           

          def calling_you_back

                      ring_sound = Phone.get_ringer

                      puts "#{ring_sound}"

                      #The following line is where issues occur, when commented it works fine

                      Phone.set_ringer("RingingRingingRinging")

                      ring_sound = Phone.get_ringer

                      puts "#{ring_sound}"

                      puts "I called you back."

          end

           

          begin

                Phone.calling_you(method(:calling_you_back))

          end