Android Question progress animation not updating inside thread

phukol

Active Member
Licensed User
Longtime User
I have a progress animation (using LoadingIndicatorView) that is running inside a thread. I call this animation when i start to update my SQLITE db. When downloading all the needed files (using httpok), the animation is running properly but when i start to insert the downloaded data inside my sqlite (records can be around 1000 to 2000), the animation freezes (the sub is still executed as i have placed logs inside the sub). I already tried using doevents but it has no effect, what can i do or implement to fix this issue?

Here is the thread i declared inside Globals:
B4X:
Dim ThreadTest  As Thread
    ThreadTest.Initialise("TT")  
    ThreadTest.Start(Me,"test_thread",Null) 

    Private np1 As NumberProgressBar

HEre is the test_thread sub (In that code i made a boolean flag that exits the sub when it is true. If not i display the progressbar):
B4X:
Sub test_thread
    If bitStopThread Then 
        Return
    Else  
        pnlProgress.BringToFront
        showProgress(True)
    End If
End Sub

this is the sub for updateing the text inside the panel of the progressbar:
B4X:
 Sub thread_progress(str As String)
     
    If bitStopProgress Then 
        Return
    Else 
        pnlProgBar.BringToFront
        showProgBar(True)
        lblProgBar.Text = str
       
                If np1.Progress <= 30 Then
                    np1.incrementProgressBy(1)
                End If 
           
        DoEvents
    End If
End Sub


I use this code inside a sub to update my SQLite db and call the progress animation:
B4X:
For intA = 0 To upInfo.Length - 1
                    If upInfo(intA).Length > 0 Then  
                        Dim myInfo() As String = Regex.Split("~",upInfo(intA))
                        thread_progress( "Updating Quizzes....")
                       
                        intRec = Starter.s.ExecQuerySingleResult("select exm_id from tbl_exam where exm_id = " & myInfo(15) )
                        If intRec = Null Then 
                                bitUpdate = True
                                Starter.s.AddNonQueryToBatch("insert into tbl_exam (exp_id,sbj_id,mode_,info_,picture_,answer_,a_,pic_a,b_,pic_b,c_,pic_c,d_,pic_d,e_,pic_e,rationale_,exm_id,exm_free ) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", _
                                    Array As Object(myInfo(16),myInfo(18),myInfo(0),myInfo(1),myInfo(2),myInfo(3),myInfo(4),myInfo(5),myInfo(6),myInfo(7),myInfo(8),myInfo(9),myInfo(10),myInfo(11),myInfo(12),myInfo(13),myInfo(14),myInfo(15),myInfo(20)   )) 
                               
                               
                        End If
                    End If
                Next

     Starter.s.ExecNonQueryBatch("SQL1")

I used the same loop to process 3 types of data so i use them around 3 times for the entirety of the program.
 

phukol

Active Member
Licensed User
Longtime User
I just want the progress animation to have a separate thread so i can update it's text value whenever i have reached a certain point. But the problem is whenever i reach the very first data to be inserted using
AddNonQueryToBatch, the progress animation stops
 
Upvote 0

phukol

Active Member
Licensed User
Longtime User
so how can i update the progress animation? is using doevents and or timer the only solution? can you guide me Erel even a short snippet?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
(You can make it much faster if you reduce the image sizes.)

You can do something like:
B4X:
CompleteCounter = 0 'process_global variables
NumberOfTasks = 0
Dim counter As Int
For i = 0 To ...
 Starter.s.AddNonQueryToBatch(...)
 Counter = Counter + 1
 If Counter = 100 Then
   NumberOfTasks = NumberOfTasks + 1
   Starter.s.ExecNonQueryBatch("sql")
   Counter = 0
 End If
Next
If Counter > 0 Then
  NumberOfTasks = NumberOfTasks + 1
  Starter.s.ExecNonQueryBatch("sql")
End If

End Sub

Sub SQL_NonQueryComplete (Success As Boolean)
 CompleteCounter = CompleteCounter + 1
 Dim Progress As Float = CompleteCounter / NumberOfTasks * 100
 
Upvote 0

phukol

Active Member
Licensed User
Longtime User
Thank you Erel but im not using any images just text data inserted into the database. Basically i fetch my data from a web service. Then i split the data and insert them into the database.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
It is not clear to me which part takes 3-4 minutes.
Probably the blocking 4000 Singlequeries? Asuming there are 4000.
For intA = 0 To upInfo.Length - 1
If upInfo(intA).Length > 0 Then
Dim myInfo() As String = Regex.Split("~",upInfo(intA))
thread_progress(
"Updating Quizzes....")

intRec = Starter.s.ExecQuerySingleResult(
"select exm_id from tbl_exam where exm_id = " & myInfo(15) ) ' Seems to be a blocking one as it will return a result (not using async tasks)
If intRec = Null Then
bitUpdate =
True
Starter.s.AddNonQueryToBatch("insert into tbl_exam (exp_id,sbj_id,mode_,info_,picture_,answer_,a_,pic_a,b_,pic_b,c_,pic_c,d_,pic_d,e_,pic_e,rationale_,exm_id,exm_free ) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", _
Array As Object(myInfo(16),myInfo(18),myInfo(0),myInfo(1),myInfo(2),myInfo(3),myInfo(4),myInfo(5),myInfo(6),myInfo(7),myInfo(8),myInfo(9),myInfo(10),myInfo(11),myInfo(12),myInfo(13),myInfo(14),myInfo(15),myInfo(20) ))


End If
End If
Next

Starter.s.ExecNonQueryBatch(
"SQL1")

ExecNonQueryBatch will only executed once. And i guess it will finish fast...

I see the problem in fetching tousands of results as a single result (and not using async methods).... And each one is holding the mainthread
 
Upvote 0
Top