Android Question About threading library

Discussion in 'Android Questions' started by marcick, May 19, 2015.

  1. marcick

    marcick Well-Known Member Licensed User

    Following the "threading library thread" i have now this basic service

    Code:
    Sub Process_Globals

        
    Dim Thread1 As Thread
       
    End Sub
    Sub Service_Create

        Thread1.Initialise(
    "Thread1")

    End Sub

    Sub Service_Start (StartingIntent As Intent)

        
    Dim args(2As Object
        args(
    0)=1
        args(
    1)="hello"
       
        Thread1.Name = 
    "B4A Thread 1"
        Thread1.Start(
    Null"Thread_Sub1", args)       
       
    End Sub

    Sub Service_Destroy
       
    End Sub

    Sub Thread_Sub1(x As Int, a As String)

        
    Log(x)
        
    Log(a)
       
    End Sub


    Sub Thread1_Ended(EndedOk As Boolean, error As String'An error or Exception has occurred in the Thread
       
        
    Log(error &  " - Thread1 Ended")
        
    Log(EndedOk)
        
    StopService("")
       
    End Sub
    Now, I have read (and see) that this library has problems in debug mode and I should compile my code as a library. Are these steps correct ?

    1) create a blank project, with just this service module
    2) compile it as library
    3) include the library in my real project
    4) from my real project, use startservice("test")

    I'm a bit confused: do I reaaly need a service ? Is it better to simply create a code module and compile it as library ?
    How to call this module and pass also some parameters ?
     
  2. DonManfred

    DonManfred Expert Licensed User

    Without knowing what exactly you are doing in this thread it is hard to give an answer.
    Normally i would say you dont need a service to run a thread
     
  3. marcick

    marcick Well-Known Member Licensed User

    Right.
    I have an http request that takes 20 seconds to download some data, then other 20 second to elaborate data received in a for-next loop.
    Sometimes I see the system message that app is not responding so I'm studying how to do the some job in a separate thread.
    Is it enough ?
     
  4. DonManfred

    DonManfred Expert Licensed User

    it cannot be the httpdownload if you are using the httputils2.
    so i guess it is that what you do after you downloaded the data.
    Yes, a thread should be good for this
     
  5. marcick

    marcick Well-Known Member Licensed User

    Ok, to be exact it is an http request in RDC that retrieve data from a remote database.
    So, to avoid problems in debug mode, how to put all the thread code in a library and call it from the main app, passin it some parameters also ?
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    The downloading time is not relevant as it is done in the background.

    Have you measured the data processing time on a real device in Release mode? Why does it take 20 seconds?

    Are you inserting the data to a SQLite database? Are you using a single transaction?
     
  7. marcick

    marcick Well-Known Member Licensed User

    Hi Erel, correct. I download 20.000 records and get the JobDOne event in a couple of seconds, the problem is not here.

    Code:
    Sub JobDone(Job As HttpJob)

        
    If Job.Success = False Then
            
    Log("Error: " & Job.ErrorMessage)
        
    Else
            
    If Job.JobName = "DBRequest" Then
                
    Log("RDC JobDone " & DateTime.Date(DateTime.Now))        ' RDC JobDone 2015/05/19 15:42:44 (ok, needed just a couple of seconds)
                Dim result As DBResult = reqManager.HandleJob(Job)        '
                Log ( "RDC total: " & result.Rows.Size & " records. " & DateTime.Date(DateTime.Now))    ' RDC total: 20000 records. 2015/05/19 15:42:58
                If result.Tag = "tag1" AND result.Rows.Size>0 Then 'query tag
                    MySub.SQL_Bls1.BeginTransaction
                    
    Try
                           
    For Each records() As Object In result.Rows
                            
    DoEventsDoEventsDoEvents
                            rec = records(result.Columns.Get(
    "ID"))
                            
    ' etc
                            ' etc
                            Dim query As String = "INSERT INTO records VALUES(NULL,?,?,?,?,?,?,?,?,?,?,?,?,?)"
                            SQL_Bls1.ExecNonQuery2(query, Scan_Record_ForDb(
    Sms))
                         
    Next
                        MySub.SQL_Bls1.TransactionSuccessful
                        
    Log ( "Web RDC finished: " & DateTime.Date(DateTime.Now))    ' Web RDC finished: 2015/05/19 15:43:22
                    Catch
                        
    Log("RDC error " & LastException.Message)
                    
    End Try
                    MySub.SQL_Bls1.EndTransaction
            
    End If
        
    End If
        Job.Release
      
    End Sub
    the line "Dim result As DBResult = reqManager.HandleJob(Job)" requires about 14 seconds and during this time the device is totally frozen.
    Then I elaborate each record and insert it in the database using BeginTransaction.
    This process requires about 24 seconds. With the DoEvents I have added, the device is more or less responsive, but anyway it appears the message that app is not responding.
    So, this is why I was looking at the threading library.
    This test is on a real device in release mode.
     
  8. Erel

    Erel Administrator Staff Member Licensed User

  9. marcick

    marcick Well-Known Member Licensed User

    Thanks, I'll see and come back.
    What about the the line "Dim result As DBResult = reqManager.HandleJob(Job)" that requires about 14 seconds and during this time the device is totally frozen ?
    Marco
     
  10. Erel

    Erel Administrator Staff Member Licensed User

    I see. Why are you downloading so many records? Why do you need to make a local copy?

    If you are making a local copy then it might be better to create the SQLite database on the server and to download it as is.
     
  11. marcick

    marcick Well-Known Member Licensed User

    Hi,
    using the SQL.AddNonQueryToBatch method, I have now about 12 seconds (half than before) in the loop that elaborate each record and insert it in the database.
    So, during the first 14 seconds (Dim result As DBResult = reqManager.HandleJob(Job)) the device is totally frozen. During the remaining 12 seconds of the loop I can choose: totally frozen or more or less resposnsive if I add the DoEvents.
    Anyway can appears the message that app is not responding.

    The MySql database on the server contains data of many users and each user have access only to some data according to specific rules.
    This is why I have decided to have a local copy on the device only of the data of interest of the user.
    Data are synchronyzed every few seconds with a Timestamp method (each time from last sync timestamp to now) so normally each synchronization retrieve a few records and there are no problem.
    But in some extreme situations may happen that there are a lot of records to sync, for example the first time the app is installed or when the app is not kept running for some days.
     
  12. marcick

    marcick Well-Known Member Licensed User

    I think I need the threading library .......
    I came back to the first question: I actually have this code that make synchronization with the server in a service module, called each 10 seconds from the main activity.
    If I modify it using the threading library, how to avoid problems in debug mode ? Do I hav eto create a blank project with just this sync service, compile it as library and include the library in the main app ?
    Consider thet this service module share also some variables with other modules.
     
  13. Erel

    Erel Administrator Staff Member Licensed User

    The real problem is not with the debugger. The problem is that RDC client code (and possibly other components) are not thread-safe. You will make your project much more complicated and less reliable.

    I still think that it will be better to create a SQLite database on the server and download it. It should be quite simple to create a server with B4J that will read the data from RDC and create a SQLite database from it which the device will download. You can at runtime decide whether you need a full download (SQLite db) or only a small update.
     
  14. marcick

    marcick Well-Known Member Licensed User

    Things become complicated ...
    So, keep away from threading library you say.
    I suppose also (off-topic) that is not possible for you to create a DoEvent function that really allow us to get rid of "app not responding" problems ...
    Marco
     
  15. Erel

    Erel Administrator Staff Member Licensed User

    Relying on DoEvents for this will be problematic.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice