Android Question What really DoEvents make?

Douglas Farias

Expert
Licensed User
Longtime User
hi all i m trying to found on the forum about do events i fint this

DoEvents
Processes waiting messages in the messages queue.
DoEvents can be called inside lengthy loops to allow the program to update its UI.
Other waiting events will not be handled by DoEvents.

but i dont found examples off use, can someone tell me what is do events and show a simple examples where use this, show this working, please i really want to know what is this *-*

if u have a simple example code show me the doevents in action pls
 

Douglas Farias

Expert
Licensed User
Longtime User
hi man, i see now but the stevel05 only say to dont is good use doevents on big loops, but i want know what exacty do events make *-*
have one code with this working? and where is recomended to use?
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
I cause the app to update itself so that you don't get these "app is not responding , close or wait?" notifications.
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
hi man, i see now but the stevel05 only say to dont is good use doevents on big loops, but i want know what exacty do events make *-*
have one code with this working? and where is recomended to use?
Hi Douglas,

One good example is the roll function I use in my Yahtzee game. I have 5 dice & I want to generate 8 random numbers for each die when the user hits the roll button. If I just set up a loop like this:

B4X:
For iCnt = 0 to 4
    For iCnt2 = 0 to 7
        imgDie(iCnt).Bitmap = LoadBitmap(File.DirAssets, Rnd(1, 7) & ".png")
    Next
Next

Then the UI will only be updated once both loops have finished. So if I want the user to see the effect of each die rolling in turn (& each number generated for each die) then I just use a DoEvents like this:

B4X:
For iCnt = 0 to 4
    For iCnt2 = 0 to 7
        imgDie(iCnt).Bitmap = LoadBitmap(File.DirAssets, Rnd(1, 7) & ".png")
        DoEvents
    Next
Next

Doing this means that every time a random number is generated & the related image is loaded, the UI will be updated & the user will see the result for a fraction of a second before the next roll completed. This gives the effect of each die rolling in turn.

Hope this helps explain it.

- Colin.
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
also notice that this creates a slowdown.

if you really need it and the loop is long don't do it on each iteration but every 100 or so.
 
Upvote 0

Loris Anoardi

Member
Licensed User
Longtime User
I'm using Doevents but i haven't success.
I want to show a progressdialogshow before executing an intense cycle for loading an array.
The problem: the progress dialog appears at the end of this cycle, even if i use DoEvents.
How can i resolve?
Thank you for help
Loris Anoardi
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
How can i resolve?
Dont block the main thread. Instead do your intensive work in another thread (maybe with the threading library or a service).

Using DoEvents CAN be a WORKAROUND in SOME cases but most probably it should not needed to use it. If you do need it then your code is probably wrong.
 
Upvote 0

Mikonios

Active Member
Licensed User
Longtime User
After several tests I get the impression that doevents generate harmful effects.
A doevents after a callsubdelay, works fine.

However, if I throw a Doevents after a SMTP.Send, blocks the execution of the event SMTP_MessageSent.

Similarly a Doevents after a OnMapLoadedCallback1.Initialize ("OnMapLoadedCallback1") stops the event ejeucion OnMapLoadedCallback1_MapLoaded

It does this mean that stops DOEVENTS event management to prioritize the message queue ??
 
Upvote 0

lemonisdead

Well-Known Member
Licensed User
Longtime User
It does this mean that stops DOEVENTS event management to prioritize the message queue ??
Not at all. But if you have events waiting they can slow down the whole process.
For example, the SMTP routine you are talking about should be better put in a service

Edit : from the documentation
DoEvents can be called inside lengthy loops to allow the program to update its UI.
Other waiting events will not be handled by DoEvents.
https://b4x.com/android/help/core.html#keywords_doevents
 
Last edited:
Upvote 0

Mikonios

Active Member
Licensed User
Longtime User
Two examples ::

1. Tasker App launched by 1:30 every day, collects data from Web's, updated SQLite databases and eventually send mail.
.- With 20 minutes to process DOEVENTS ago, and after nine hours not send the mail.
I open the device, the app appears in the foreground and
automatically launches the mail and finish the croissant. NotOk.
.- Eliminated DOEVENTS and send mail immediately ending the App. Ok.

2. gmaps App
.- With DoEvents after 45 minutes OnMapLoadedCallback1_MapLoaded not launch event. NotOk.
.- Eliminated DOEVENTS OnMapLoadedCallback1_MapLoaded event and launches immediately. Ok.

So how long it slows down with DOEVENTS App?

Edit:: The process shows me that DOEVENTS stops natural events.
 
Last edited:
Upvote 0
Top