B4J Question ui with progressbar not updating

xulihang

Active Member
Licensed User
Longtime User
I write a static code module which works as a progressDialog. It works well. But this time, when I am trying to update the cloudkvs with a local keyvaluestore, it just does not update.

B4X:
Sub Class_Globals
 Private fx As JFX
 Public translationMemory As KeyValueStore
 Public externalTranslationMemory As KeyValueStore
 Private sharedTM As ClientKVS
 Private similarityStore As Map
 Public currentSource As String
 Private projectName As String
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize(projectPath As String)
    translationMemory.Initialize(File.Combine(projectPath,"TM"),"TM.db")
    externalTranslationMemory.Initialize(File.Combine(projectPath,"TM"),"externalTM.db")
    similarityStore.Initialize
    initSharedTM(projectPath)
End Sub

Public Sub initSharedTM(projectPath As String)
    If Main.currentProject.settings.GetDefault("sharingTM_enabled",False)=True Then
        projectName=File.GetName(projectPath)
        Log("projectName"&projectName)
        Dim address As String=Main.currentProject.settings.GetDefault("server_address","http://127.0.0.1:51042")
        sharedTM.Initialize(Me, "sharedTM", address,File.Combine(projectPath,"TM"),"sharedTM.db")
        sharedTM.SetAutoRefresh(Array(projectName&"TM"), 0.1) 'auto refresh every 0.1 minute
        Dim job As HttpJob
        job.Initialize("job",Me)
        If address.EndsWith("/")=False Then
            address=address&"/"
        End If
        job.Download(address&"getinfo?type=size&user="&projectName&"TM")
        wait for (job) JobDone(job As HttpJob)
        If job.Success Then
            Try
                Dim size As Int=job.GetString
                If size=0 Then
                    fillSharedTM
                End If
            Catch
                Log(LastException)
            End Try
        End If
        job.Release
    End If
End Sub

Sub fillSharedTM
    Dim tmmap As Map
    tmmap=sharedTM.GetAll(projectName&"TM")
    Dim size As Int=translationMemory.ListKeys.Size
    progressDialog.Show("Filling SharedTM","sharedTM")
    Dim index As Int=0
    For Each key As String In translationMemory.ListKeys
        index=index+1
        Log(index)
        Log(size)
        If tmmap.ContainsKey(key) Then
            If tmmap.Get(key)<>translationMemory.Get(key) Then
                sharedTM.Put(projectName&"TM",key,translationMemory.Get(key))
            End If
        Else
            sharedTM.Put(projectName&"TM",key,translationMemory.Get(key))
        End If
        progressDialog.update(index,size)
        Sleep(0)
    Next
    progressDialog.close
End Sub


The progressDialog code:

B4X:
'Static code module
Sub Process_Globals
    Private fx As JFX
    Private frm As Form
    Private Label1 As Label
    Private ProgressBar1 As ProgressBar
    Private progressTypeValue As String
End Sub

Sub Show(title As String,progressType As String)
    frm.Initialize("frm",600,200)
    frm.RootPane.LoadLayout("progress")
    frm.Title=title
    progressTypeValue=progressType
    frm.Show
End Sub

Sub ShowWithoutProgressBar(title As String,progressType As String)
    frm.Initialize("frm",400,120)
    frm.RootPane.LoadLayout("progress")
    frm.Title=title
    Label1.Top=60-Label1.Height/2
    ProgressBar1.Visible=False
    progressTypeValue=progressType
    frm.Show
End Sub

Sub update(completed As Int,segmentSize As Int)
    ProgressBar1.Visible=True
    Label1.Text=completed&"/"&segmentSize
    ProgressBar1.Progress=completed/segmentSize
End Sub

Sub update2(info As String)
    Label1.Text=info
End Sub

Sub close
    frm.Close  
End Sub

Sub frm_CloseRequest (EventData As Event)
    If progressTypeValue="pretranslate" Then
        Main.currentProject.completed=Main.currentProject.segments.Size
        fx.Msgbox(frm,"The process is canceled.","")
    End If

    Return
End Sub
 

xulihang

Active Member
Licensed User
Longtime User
It will be much faster if you create a List with all the data and add it with a single call to Put.

I edited the code but not sure if I am doing it right. The progress still doesn't update sometimes. Now I just let the program not responding during this processing and give a hint to the users before this.

B4X:
Sub fillSharedTM
    progressDialog.Show("Filling SharedTM","sharedTM")
    Dim tmmap As Map
    tmmap=sharedTM.GetAll(projectName&"TM")
    Dim size As Int=translationMemory.ListKeys.Size
    Dim index As Int=0
    Dim toAddMap As Map
    toAddMap.Initialize
    For Each key As String In translationMemory.ListKeys
        index=index+1
        Sleep(0)
        progressDialog.update(index,size)
        If tmmap.ContainsKey(key) Then
            If tmmap.Get(key)<>translationMemory.Get(key) Then
                toAddMap.Put(key,translationMemory.Get(key))
            End If
        Else
            toAddMap.Put(key,translationMemory.Get(key))
        End If
    Next
    fillALL(toAddMap)
    progressDialog.close
End Sub

Sub fillALL(toAddMap As Map)
    For Each key As String In toAddMap.Keys
        sharedTM.Put(projectName&"TM",key,toAddMap.Get(key))
    Next
End Sub
 
Last edited:
Upvote 0

xulihang

Active Member
Licensed User
Longtime User
B4X:
Sub fillALL(toAddMap As Map)
    For Each key As String In toAddMap.Keys
        sharedTM.Put(projectName&"TM",key,toAddMap.Get(key))
    Next
End Sub

I add a sleep(0) to this sub and it loads fast, not blocking the ui thread.

B4X:
Sub fillALL(toAddMap As Map)
    For Each key As String In toAddMap.Keys
        sleep(0)
        sharedTM.Put(projectName&"TM",key,toAddMap.Get(key))
    Next
End Sub
 
Upvote 0

Harris

Expert
Licensed User
Longtime User
I add a sleep(0) to this sub and it loads fast, not blocking the ui thread.

B4X:
Sub fillALL(toAddMap As Map)
    For Each key As String In toAddMap.Keys
        sleep(0)
        sharedTM.Put(projectName&"TM",key,toAddMap.Get(key))
    Next
End Sub
That was what I was going to suggest since I had the same issue with ProgressDialogShow2(" Loading List of Images...",False)
The addition of sleep(0) in the loop allowed it to show and spin....
 
Upvote 0
Top