B4J Question Label not refreshed inside

adjie

Member
Licensed User
Longtime User
I have simple timer that print counter in label. The problem is label not imidiately changed the value. Anyone can help ?

here is the code

B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private Label1 As Label
    Private Button1 As Button
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.SetFormStyle("UNIFIED")
    MainForm.RootPane.LoadLayout("l1") 'Load the layout file.
    MainForm.Show
End Sub

Sub Button1_Action
    Dim n As Int
    For n = 0 To 5
        Label1.Text = n
        Log(n)
        Delay(500)
    Next
End Sub



Sub Delay (DurationMs As Int)
Dim target As Long = DateTime.Now + DurationMs
Do While DateTime.Now < target
Loop
End Sub

Note: Sory for the title, it should be "Label not refreshed inside for next loop"
 
Last edited:

adjie

Member
Licensed User
Longtime User
You should never try to hold the main thread. Nothing good will come out of this. Use a timer with an interval of 500ms.
Actually what I want to do with my code is I want to show the progress of the process inside the For Next loop. My code above is just to simplify the process. The main application is I'm reading a table of database and convert it another database. Any sugesstion ?

note: in vb6 I can simply do this

B4X:
For n = 0 To rs.RecordCount
        'in this line the code for inserting to other database
        Text1.Text = n & "/" & rs.RecordCount
    Next
 
Last edited:
Upvote 0

adjie

Member
Licensed User
Longtime User
Hi,

Sory for the late reply. My code is quite simple.
Here is one of its process :
B4X:
Dim Cursor1 AsCursor
Cursor1 = SQL1.ExecQuery("SELECT id FROM table1")
For n = 0 To Cursor1.RowCount - 1
  SQL1.ExecNonQuery("update table2 set flag = 'readed' where id = '" & Cursor1.GetString("col1") & "'")
  Text1.Text = n+1 & " of " & Cursor1.RowCount
Next
I want to show the user the progress of the process.

Before this, I was trying to use progress bar, but have no luck with that, so I turn using TextBox. But also no luck as well.
any solution for this ?
 
Upvote 0

adjie

Member
Licensed User
Longtime User
Create a transaction and it will be fast enough that you won't need to do show any indication to the user.
I understand that Erel, but still just want to show the progress as the previous application in VB6 did that. Did you mean that I shouldn't show any progress with that process ? Is there any option that I can do to showing any progress information to user ? I need this because I'm trying to replace my previous app in vb6 to b4j without any change on the visual side.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
The UI will only be updated when the main thread is free to process the message queue. Try it with a transaction. My guess is that it will be too fast to show any progress.

If the task is really long then you should use SQL.ExecNonQueryBatch and all the SQL commands will be executed in the background. You will not be able to show a real progress indicator as it only raises an event when it completes. You can show an indeterminate ProgressBar by setting the Progress property to -1.
 
Upvote 0

adjie

Member
Licensed User
Longtime User
Still confuse with "main thread" things. Just tryin to understand, Did you mean I should run the process in other thread and pass or raise event that contain the progress to main thread ?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Did you mean I should run the process in other thread and pass or raise event that contain the progress to main thread ?
No.

You have two options:
1. Create a transaction. It will be executed on the main thread but it will be 100+ times faster so it might be fast enough.
2. Use the Async methods. They are explained here: https://www.b4x.com/android/forum/threads/sql-tutorial.6736/#content
The async methods are internally executed by a background thread so they don't slow down the main thread. You will not be able to show the actual progress but at least the UI will be responsive.
 
Upvote 0
Top