Android Question Thread safe lists

SteveTerrell

Active Member
Licensed User
Longtime User
I have an application where a service is collecting data from a bluetooth source (using AsyncStreams). The data is to be processed and displayed on a bitmap which can, depending on the latest data values, take some time to redraw - on occasions longer than the interval between BT updates.

I presume that the service can pre-empt the GUI code so it is not particularly safe to use a global map or list where the service adds data and the GUI reads and displays it.

Is there a locking mechanism that I can use or should I send new packets of data to the GUI using callsubdelayed and hope that the GUI does not get too many packets behind the data gathering?
 

SteveTerrell

Active Member
Licensed User
Longtime User
A bit surprising, yes! But interesting and useful information.

Is there any pre-emption anywhere?
 
Upvote 0

SteveTerrell

Active Member
Licensed User
Longtime User
Just so that I can get this clear in my head:-

I have a GUI sub responding to a CallSubDelayed from a service that receives new data over bluetooth. The GUI sub can take up to 1 second to recalculate and draw the new data on a bitmap then Gaussian blur and display it.

The service has a timer tick sub (100ms period) which is supposed to requests new data on a time basis (normally about every 0.5s) and an AsyncStream new data sub to receive the data.

Is it the case that once the GUI sub runs the service will not respond to the timer tick until the GUI has finished (returned) thus making some of the requests having a longer gap than others.

Would DoEvents in the GUI sub be needed to allow the service to run and keep some accuracy in its timing?
 
Upvote 0

SteveTerrell

Active Member
Licensed User
Longtime User
Thanks for the update, I had avoided the threading library because of the warnings that it should not be used for GUI manipulation. Presumably though this only applies to code such as the final drawing of a bitmap on a canvas initialised to a panel. Is the earlier creation of the bitmap using code like this safe in a thread?

B4X:
Sub CreateScaledBitmap(Original As Bitmap, width As Int, height As Int, Filter As Boolean) As Bitmap
  Dim r As Reflector
  Dim B As Bitmap
  B = r.RunStaticMethod("android.graphics.Bitmap", "createScaledBitmap", _
      Array As Object(Original, width, height, Filter), _
      Array As String("android.graphics.Bitmap", "java.lang.int", "java.lang.int", "java.lang.boolean"))
  Return B
End Sub

As an alternative, would a chain of CallSubDelayed be a way of allowing other events to be processed in the thread (does the callsubdelayed put the run request at the end of the queue of events that would be building up on my thread)?
 
Upvote 0

SteveTerrell

Active Member
Licensed User
Longtime User
This code can be executed on a background thread.

Note that the first step is to measure (in release mode on a real device) what exactly takes so long to run.

You can use CallSubDelayed to split a large task into several smaller tasks.

Thanks for all the help
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…