Android Question How to use a thread in a Service

Alberto Michelis

Well-Known Member
Licensed User
I trying to make my service run in another thread using the Threding lib
I did the following:

B4X:
Sub Process_Globals
   Dim Thread1 As Thread
   Dim Lock1 As Lock
   Dim end1 As Int
   Dim ok As Boolean
   Dim msg As String
I move al my Service_Create code to the ThreadSub1 and replace it with the start thread code
B4X:
Sub Service_Create
   Thread1.Initialise("Thread1")
   Dim args(0) As Object
   Lock1.Initialize(True)
   Thread1.Name = "B4A Thread 1"
   Thread1.Start(Null, "ThreadSub1", args)    
End Sub
I initiate a parser, an Sql object and a timer and run the timer_tick
B4X:
Sub ThreadSub1
   parser.Initialize
   SQL1.Initialize(File.DirDefaultExternal, "portit.db", True)

  Timer1.Initialize("Timer1", 10000) ' 1000 = 1 second
  Timer1.Enabled = True

  Timer1_Tick
  
End Sub

Sub Thread1_Ended(fail As Boolean, error As String) 'An error or Exception has occurred in the Thread
   Msgbox(error, "Thread1 Ended")
End Sub
When I run it I get an inmediate Thead_Ended error:

Exception : wrong number of arguments; expected 4, got 0

How can I do it?
Thanks
 

Alberto Michelis

Well-Known Member
Licensed User
I need to read an parse a web page to a Sql db.
I need to do it every 10 seconds and it last about 1 or 2 seconds.
I do it with a service and its timer.
While my app is running and the user is seing, lets say, a list of prices...
When the service is doing this the UI stops to repond, if the user are swiping the list up and down the scroll stops for this period of time and hen goes back again and I dont want this to happen.
 

marcick

Well-Known Member
Licensed User
If it works in Release, then it's a known problem of the threading library with debug mode.
 

freedom2000

Well-Known Member
Licensed User
Yes it is a known behavior of threading lib.

It doesn't work with rapid debugger but it does work with legacy debugger

try it and you won't have this exception : Exception : wrong number of arguments; expected 4, got 0
 
Top