Android Question what problem in this code? (( ProgressDialogShow ))

khosrwb

Active Member
Licensed User
Longtime User
I will use ProgressDialogShow , but don't work!!!
 

Attachments

  • p.zip
    6.6 KB · Views: 164

ilan

Expert
Licensed User
Longtime User
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. ;)

this is not a good idea run DoEvents in a loop of 100k size o_O
maybe he should run this code in a service and the progressdialog will run as expected.

to be honest, i never understood that progessdialog thing. if you want to show the progressdialog you could run it before you call a different sub with callsubdelayed but it will freeze and when the code is finished it will run again. but i would expect it to run while the code is executing.

the only way to do it is running your code in a service but this is not very practical.

anyway to DoEvents inside a Loop will only make it very very very very...... SLOW!!
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
I agree, but it seems he's just testing something.

i think he wants to understand how to show a RUNNING progressdialog and not freeze while app is working in background. so a service should be used (in my opinion :))
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
its weird in a service its also not running. :confused:
dont understand that progressdialog stuff


but still the time is a big difference

with Doevents (go through 10.000 items) took 54 seconds
without doevents (go through 1.000.000) so x10 then the first test it took 5 seconds
 
Upvote 0

RandomCoder

Well-Known Member
Licensed User
Longtime User
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.
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
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.

i am using a service but why do i need to update every x time the loop? i know i could do it with:

B4X:
if i mod 200 then DoEvents

but i would like to have a progressdialog animation running when i start a process and until i finish it and not freeze because process is running in background.
the WHOLE point of a progressdialog is to give the user the filling that the app did not freeze while you perform a big process but what happens is that the progress dialog freezes by it self o_O

to solve it with doevents inside a loop and make the loop like this very very slow is not a solution.
there need to be a RUN IN BACKGROUND solution where the app is not waiting until that task is finished.
you have such Background Tasks in VB.

i thought a service is running in background but using a service will still freeze the Progressdialog until the code in the Service is finished.

this does not make any sense to me, sorry
 
Upvote 0

RandomCoder

Well-Known Member
Licensed User
Longtime User
I've created a small example attached. On my Samsung Galaxy S3 I can set the refresh rate to as low as 1,000 iterations (although 500,000 is a more sensible value), it does not make sense to set the ProgressBar to refresh too frequently as it will only slow the execution of the program because of the additional overhead of having to refresh the display. Also be aware that you can cause the program to fault if CallSub is called too frequently and the system is unable to process all of the requests!
This example simply strobes the ProgressBar until complete, you could add a label which shows how many files have been read or calculations performed (whatever it is that you are doing), or you could scale the ProgressBar to give an accurate 0 to 100 scale of the function being performed. You could also chose to refresh the ProgressBar once every second or half second which might actually be better as the refresh rate would be more consistent. The attached is just an example that can easily be tailored to your exact requirements. For those that don't have version 5.8 I have also provided the code below...

Main module...
B4X:
#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

Starter Service...
B4X:
#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

Kind regards,
RandomCoder
 

Attachments

  • Progressbar_Example.zip
    7 KB · Views: 179
Upvote 0

ilan

Expert
Licensed User
Longtime User
@RandomCoder i think u missunderstood the thread. We r not talking about a progressbar. We r talking about progressdialog!!

The problem is that the animation of the dialog freeze until the process is finished and that is a wrong behavior.

The whole point of the progressdialog is to give the user the feeling that the app is running and not freezed. But if the progressdialog animation is freezed then why use it?? And call inside the loop DoEvents (what will update the progressdialog and the animation will work) will make my loop very very slow!
 
Upvote 0

RandomCoder

Well-Known Member
Licensed User
Longtime User
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.
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
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.

running it in a different thread is the answer i guess but i would like when i call: ProgressDialog.Show that it will do it automatically without creating to much stuff around it and without sacrifice any processing time. the whole point of that Dialog is to give the user the filling that the app is still running and that it is not freezed.

the dialog should not be effected by the activity process it should start and end when i call it and run as long i did not call hideprogressbardialog
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
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.

The task is a simple loop from 0 to 1.000.000
And while this loop is running we would like to see the progressdialog animation (without calling doevents each x times and make the loop much slower). Is this possible?

Thanx :)
 
Upvote 0
Top