You need to add DoEvents inside the loop.
I hope you understand that.B4X:ProgressDialogShow2("please wait ...",False) For i=0 To 100001 DoEvents '<------ Here i=i+1 i=i+1 Log(i) Next
I hope you understand that.B4X:ProgressDialogShow2("please wait ...",False) For i=0 To 100001 DoEvents '<------ Here i=i+1 i=i+1 Log(i) Next
I agree, but it seems he's just testing something.
You should use a Service and call a Sub in the Main Activity to update the progress dialogue every 200th iteration of the loop or whatever looks best.
if i mod 200 then DoEvents
#Region Project Attributes
#ApplicationLabel: Progressbar Example
#VersionCode: 1
#VersionName:
#SupportedOrientations: unspecified
#CanInstallToExternalStorage: False
#End Region
#Region Activity Attributes
#FullScreen: False
#IncludeTitle: True
#End Region
Sub Process_Globals
End Sub
Sub Globals
Dim Progress As ProgressBar
End Sub
Sub Activity_Create(FirstTime As Boolean)
Progress.Initialize("Progress")
Progress.Visible = True
Progress.Color = Colors.Red
Activity.AddView(Progress, 20dip, 20dip, Activity.Width - 40dip, 50dip)
End Sub
Sub Activity_Resume
CallSub(Starter, "LongLoop")
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub ProgressBar_Update(Value As Int)
Progress.Progress = Value
End Sub
#Region Service Attributes
#StartAtBoot: False
#ExcludeFromLibrary: True
#End Region
Sub Process_Globals
End Sub
Sub Service_Create
End Sub
Sub Service_Start (StartingIntent As Intent)
End Sub
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
Return True
End Sub
Sub Service_Destroy
End Sub
Sub LongLoop
Dim limit As Long = 1000000000
Dim refreshrate As Int = 500000
Dim barvalue As Int = 0
For x = 0 To limit
' Work code goes here
' ...
' ...
' ...
' Update progressbar with desired refresh rate
If (x mod refreshrate) = 0 Then
barvalue = barvalue + 1
If barvalue > 100 Then barvalue = 0
CallSub2(Main, "ProgressBar_Update", barvalue)
DoEvents
Log(barvalue)
End If
Next
CallSub2(Main, "ProgressBar_Update", 100)
ToastMessageShow("LOOP COMPLETE", True)
End Sub
I'm sorry @ilan I had not realised that there was a difference between ProgressBar and ProgressDialogue, I should have checked the example in post 1. The theory should be petty similar though. I think that you will always have to sacrifice some processing time to refresh the display unless you use the threading library and run the work code on a completely different thread.
Using a service will not make any difference. The solution depends on the task itself. I might have missed it but I don't see any information about the task.