how to flash a point

Ben

Member
Licensed User
Longtime User
To make a small line flashing I use the following code

For j=1 To 4
If j=1 OR j=3 Then co=Colors.black Else co=Colors.white
Canvas.DrawLine(x1,y1,x2,y2,co,5)
pnl. invalidate
t= DateTime.Now+500
Do While DateTime.Now <t
Loop
next

it seems to work correctly when using the debugger turning the white color on and off with a waiting time of 500, but when running the program I only see the final white color despite I have changed the waiting time between flashes to very diferent values (from 50 to 5000).
Do you know how to do it.
 

thedesolatesoul

Expert
Licensed User
Longtime User
It works fine with the timer. I do not very well why the "Do while" didnt work but many thanks for your fast answer.
Maybe it would have worked by adding a DoEvents inside the Do...While loop. But I dont like keeping the thread busy all the time, so I prefer Timers.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
I do not very well why the "Do while" didnt work but many thanks for your fast answer.
Because, like Windows and just about every other GUI, the Android GUI is single threaded and event driven and drawing the UI is done by putting messages in a message queue and processing them in a message loop. Android runs your message loop when your code has returned from any event Sub. If you sit in a loop in your Sub no messages can be processed so the GUI never gets updated.

If you call DoEvents in a loop the DoEvents method runs a message loop to process any GUI messages and in this case the GUI will be updated.

You should avoid writing code that waits for any time at all. If you do and if you wait for too long Android detects it and force closes your application.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…