Playing around with do loops

Scantech

Well-Known Member
Licensed User
Longtime User
Any clever guys know how to get out of the loop?

Sub Activity_Pause (UserClosed As Boolean)
UserCanceled = true
End Sub

Sub LoopTest
Do until UserCanceled = true
doevents

loop
end sub

If it was VB6 or .net no problem. Android is a little tricky for me. I want to stop this loop anytime the user clicks on home or back key. This loop will continue to run in the background and when you try reloading the app, you get blank screen.

How can I stop a loop when User hits the Back/Home Key? Thank you
 

Kevin

Well-Known Member
Licensed User
Longtime User
I would consider finding another way to accomplish what you are trying to do.

Looping like that is generally frowned upon in Android because it can cause your app to force-close due to being unresponsive.

Edit to add that perhaps you can use a timer? I think this is the preferred alternative to looping while waiting for something to happen.
 
Last edited:
Upvote 0

margret

Well-Known Member
Licensed User
Longtime User
Do Loops

The loop will consume the processor and the app may never see the home or back key. Do while and Do until are fine if you have a condition that you know will exit the loop quickly. Like:

x=0
Do while x < 10
x = x +1
loop

If you can post more about what you are trying to do, we may be able to offer more help. Also, DoEvents will never fire in a Do Loop. DoEvents only fires when there is a break/pause in the code.

Thanks,

Margret
 
Last edited:
Upvote 0

agraham

Expert
Licensed User
Longtime User
Also, DoEvents will never fire in a Do Loop. DoEvents only fires when there is a break/pause in the code.
Actually it will work and will process waiting messages in the message queue although some it will push back as it does not permit all types of message to be actioned. About the only valid use of DoEvents is to allow the UI to be redrawn in a loop in response to some change that the code has made. However as has been pointed out above it is bad practice to use a loop to wait for something in Android - and also in Windows, as both are event driven environments. Use a Timer or an event instead. If you want background working use a Service or a Thread.
 
Upvote 0

nfordbscndrd

Well-Known Member
Licensed User
Longtime User
...Also, DoEvents will never fire in a Do Loop. DoEvents only fires when there is a break/pause in the code.

Actually, the purpose of DoEvents is to allow other code outside the current block of code (not limited to loops) to execute when there is NOT a break/pause. When there IS a break/pause, the other code always has a chance to execute.

See this page in the Wiki which says:
DoEvents can be called inside lengthy loops to allow the program to process waiting events.
 
Upvote 0

margret

Well-Known Member
Licensed User
Longtime User
Do Loops

Sorry Guys,

You are the experts and the docs are as you say. I guess I am doing something wrong in my code. I never get the DoEvents to fire unless the code is setting waiting on user input, like a panel is loaded with Buttons and is waiting on a click. Sorry, If I mislead. Guess I'am the one that needs some help.:BangHead:
 
Upvote 0

Scantech

Well-Known Member
Licensed User
Longtime User
For/Next and Do/loop comes in handy. But use it with caution on Android Device.

Lets say. I need to run a For/Next event which requires 10-15 seconds on a realtime device. When the User hits the Home Key then your app will be useless until the For/Next event has been completed. I am sure there are ways around this, but I get into habits of using For/Next and Do/Loops alot lately.
 
Upvote 0

margret

Well-Known Member
Licensed User
Longtime User
Do Loop & DoEvents

What do you mean by "fire". Perhaps you are misunderstanding what it actually does.

I have code where I wanted to update the screen/panel while I was in a long Do - Loop. In this loop I would update a value of a Label, etc. I would issue a DoEvents in this loop and the label would never update until the loop was complete. By fire, I mean process or trigger - do it's job.

I've read that DoEvents is for use in a loop to update the UI. I may just be it using wrong but I have never had it work until the loop or the for-next completes. One sample I tried was:

B4X:
MyCounter = List1.Size -1
Dim CurrentV as Int
CurrentV = 0
Do while CurrentV < MyCounter
     '** code to check dates and update AR
     '**
     '**
     Label1.Text = "Working on Record: " & CurrentV
     DoEvents 
     CurrentV = CurrentV + 1
Loop

Thanks,

Margret
 
Upvote 0

Scantech

Well-Known Member
Licensed User
Longtime User
I have code where I wanted to update the screen/panel while I was in a long Do - Loop. In this loop I would update a value of a Label, etc. I would issue a DoEvents in this loop and the label would never update until the loop was complete. By fire, I mean process or trigger - do it's job.

I've read that DoEvents is for use in a loop to update the UI. I may just be it using wrong but I have never had it work until the loop or the for-next completes. One sample I tried was:

B4X:
MyCounter = List1.Size -1
Dim CurrentV as Int
CurrentV = 0
Do while CurrentV < MyCounter
     '** code to check dates and update AR
     '**
     '**
     Label1.Text = "Working on Record: " & CurrentV
     DoEvents 
     CurrentV = CurrentV + 1
Loop

Thanks,

Margret


I copied the code and ran it on the emulator. It works perfectly fine on the emulator (which I assume is slower then real device). The label was updating at all times.
 
Upvote 0

nfordbscndrd

Well-Known Member
Licensed User
Longtime User
Margret -- I think that there may be something quirky with DoEvents in Android. Your DoEvents code *should* work. In Android 2.2, I don't have any trouble with screens/views not being completely drawn, but in Android 3.2, it happens all the time, even if I put ViewName.Invalidate to force it to redraw, so I end up with stuff like this:

B4X:
For i = 0 to 1
    Panels(x).Invalidate
    DoEvents
Next

This works, but it can cause the panel to flash/blink, but that's better than leaving it half-drawn.
 
Last edited:
Upvote 0

margret

Well-Known Member
Licensed User
Longtime User
Do Loops

I copied the code and ran it on the emulator. It works perfectly fine on the emulator (which I assume is slower then real device). The label was updating at all times.

Part of mine may be that I am working with a Froyo highly changed device. The NookColor. There are a lot of things on it that are not standard Android. That may be part of it. It was only built as a Ereader but now people are asking it to do more. I have not tried it on the emulator because I just don't like working with it and it's really slow. I do have a new ASUS Eee Pad that I will try it on. May just be the Nook that it don't work on.

Thanks,

Margret
 
Upvote 0
Top