Ruby thread on Android with rhodes 5.0.30

L Louis Mauchet 3 years 6 months ago
1 4 0

Hi,

I saw several old discussions about the impossibility of using the ruby threads on Android with rhodes (, etc...). Is that issue fixed in the new versions of rhodes? If not, is there a workaround? I need to launch a server socket in a separate thread which will listen for client connexion so i don't see how to do that without using thread... It works well on IOS & Windows but with Android i have the famous message "JNIEnv is not set for this thread!!!". Note that i get this error message when i'm trying to read a file in this separate thread.

Thanks in advance!

Louis 

Please Register or Login to post a reply

4 Replies

J Jon Tara

Are you writing your own code from scratch, and is it fairly simple? My own application was XMPP messaging and I used a large existing Gem (XMPP4R) so I don't have the option below. So, no practical work-around for me, I had to use a thread and so it can't work on Android.

If you are writing your own code, though, Ruby IO has support for non-blocking read. You will have to periodically poll. You can easily do that with a timer callback. You will get (and should catch) an exception if there is nothing to read.

You could poll pretty often, since it's going to do almost no work if there's nothing to receive. It will check the socket, find nothing, and schedule the next timer callback.

L Louis Mauchet

Hi Jon,

Thank you for your answer! You're right maybe i can try to use the combination of Timer and non-blocking socket to get something which cover my needs. But it's really inconvenient compared to the facility of using threads... After a deeper analyse, it seems that the root cause of this problem is trying to open a file in a thread on android. I've tried the following code :

def test_thread_file
    Thread.new{
      Rho::Log.info("Start thread", "DEV")

      Rho::Log.info("Try to open file", "DEV")
      file = File.new(File.join(Rho::Application.publicFolder, "css/android.css"))
      Rho::Log.info("Try to open file - OK", "DEV")

      Rho::Log.info("Try to read file", "DEV")
      read = file.read()
      Rho::Log.info("Try to read file - OK", "DEV")

      Rho::Log.info("Data file : #{read}", "DEV")

      Rho::Log.info("The End", "DEV")

    }
  end

And the error occured on line 6 (during the File.new). But if I execute the same code without using a separate thread, it works well.
Any idea of what is going on?

Thanks

Louis

J Jon Tara

Good to know at least it works on Windows too! (Do you mean Windows desktop, though, CE, Mobile, or Windows Phone?)

L Louis Mauchet

I meant Windows Desktop (8.1)

CONTACT
Can’t find what you’re looking for?