Exiting For Loop

Saj

Active Member
Licensed User
Is it possible to exit a for loop when the screen is touched? I have tried setting a variable (penOnScreen) to True when the screen is touched but this doesn't stop the for loop:

Sub frmMain_MouseDown (x,y)
penOnScreen=True
End Sub

Sub forLoop
For i=0 To nPoints
If penOnScreen Then Exit
...
Next
End Sub

Any suggestions?
Cheers

Saj.
 

agraham

Expert
Licensed User
Longtime User
I assume penOnScreen is a global variable.

To understand why this isn't working you need to know a bit about how Windows works. Windows programs are event driven and do things in response to messages received from the operating system. Every application has a message loop, which you can't see in Basic4ppc, which is where your application sits when it isn't running code. For example when it runs off the end of App_Start it enters the message loop where it waits for an operating system message. When a message arrives the message loop identifies what the message is and calls the appropriate event Sub in your application. When your event Sub ends your application returns to the message loop. You can think of the message loop code as an operating system Sub which calls your application event Subs when needed.

In your case the messages are not being received, so penOnScreen is never set, because you are running a tight loop and your application never returns to the message loop to process messages. Tight loops waiting for something to happen are bad design in event driven applications and really bad design in portable device applications as they burn processor cycles and so eat battery power. If you really need to allow messages to be processed in a loop you can call DoEvents which enters the message loop, processes any messages, calls your appropriate event Subs then returns. The danger is that DoEvents can cause some of your event Subs to be re-entered which is usually a "bad thing". With proper design you should hardly ever need to use DoEvents.
 

Saj

Active Member
Licensed User
Hi agraham

Your assumption is correct. What I am trying to do is draw a track of recorded lat/lon points on a moving map. As the number recorded points increase, the less reponsive the device gets. I need a method of getting out what the program is curently doing and respond to the user as a higher priority.

You mentioned that calling DoEvents can cause erratic behaviour and enter unexpected Subs. Is it possibe to detect which Subs are entered?

You also say that with 'proper design' DoEvents should be unnecessary. How should I go about making the program structure better designed?

Thanks

Saj.
 
Last edited:

Saj

Active Member
Licensed User
Hi Ariel Z
Its basically a nav program which I have been developing for a while. It displays a moving map (jpg) and draws where you been as a track along with your current position. The aim is to update the drawn track and current posn every second. Peformance issues arise when the total number points captured and then filtered to be displayed exceed a certain number resulting in the 1 second update rate deteriorating and it device becomes slow to respond to the touch screen.
Cheers
Saj.
 

Ariel_Z

Active Member
Licensed User
I guess you are using a timer with interval 1000?
I guess the best solution is to use a timer. Then you update n last points. How often does the last, say, 50 points change? (in other words, is it supposed to be used in a vehicle, or in an airplane?) If not to often, you can decrease the number of updates so that it does not cause performance loss. There are other performance related changes you can take, but lets start here. Could you let me know if it worked?
 

klaus

Expert
Licensed User
Longtime User
To be able to answer your question we need more information or better some code sample you should post.

How is your program working ?
You get a new point every second.
What are you doing with it ?
Do you save it somewhere?
I am afraid that you redraw all the points onto the screen? This could result in speed trouble because the more points you have the longer it takes to redraw ALL of them?
Are the points drawn onto the forelayer or onto the background or onto a bitmap and then transfered to the background layer?
How do you move your map on the screen?
Do you load your map on a bitmap and draw on it and then copy the needed part of it onto the screen?
In that case you shouldn't have trouble because you just need to add the last point to the whole drawing and copy the needed part onto the screen.

As you see with all these questions we need more information on what your program is doing and how it does it.

Best regards.
 

Saj

Active Member
Licensed User
You get a new point every second. What are you doing with it ? Do you save it somewhere? I am afraid that you redraw all the points onto the screen? This could result in speed trouble because the more points you have the longer it takes to redraw ALL of them?Are the points drawn onto the forelayer or onto the background or onto a bitmap and then transfered to the background layer?
A new position is stored in an array in degrees if:
(a) the new posn has moved by a certain distance from the last stored postion OR
(b) the heading has changed since the last stored position OR
(c) no new position has been captured in the last X seconds

Now for drawing the track I do the following to minimise the number of positions that have to be calculated on in order to determine the corresponding pixel positions on the screen, which is very time consuming:
(a) calculate the border of the screen interms of lat and lon (degrees)
(b) disregard points from the array that fall outside the screen border
(c) for positions that fall within the screen, depending on the zoom level, I disregard more points before drawing the track using agraham's ImageLibEx drawer to draw on the forelayer.

How do you move your map on the screen?Do you load your map on a bitmap and draw on it and then copy the needed part of it onto the screen?
The map is loaded into an ImageList array and then drawn onto a form using:
frmMap.DrawImage(ImageList1.Item(mapIndex),X1,Y1, X2,Y2)
X1,Y1 & X2,Y2 are calculated based on the current lat/lon position and zoom level, then used to re-draw the JPG every second, i.e. move the map.

In that case you shouldn't have trouble because you just need to add the last point to the whole drawing and copy the needed part onto the screen.
I haven't tried this, do you have any examples?
 

klaus

Expert
Licensed User
Longtime User
Last edited:
Top