Orasanu Iuliana wrote:
Ok, I know what you mean, but I have to make a big piece of code that
works in a loop.
Do you *have* to do it that way, or is it just that you are planning to
do it that way because that seems easiest?
My application has a start and a top button.
At a tap on the StartButton a call my function that works with a loop,
and if I tap on the StopButton -> I should stop my loop and return.
My question was how do I catch this event?
The normal way would be to check for it in your main event loop.
My current code -> obviuosly is not working - because to get the
StopButton event it should first exist the loop... :-(
I'm now trying to find something relevant in the threading code...
should I create a second thread?
There is nothing relevant in the threading code. You cannot create
a second thread. Palm OS is not a multitasking operating system in
the sense that applications cannot have more than one thread of
execution at once. It's just not supported.
Does anyone have some ideas?
Often, the easiest thing to do is to break your loop up into multiple
steps, then create some variables that determine the state of your loop
so that you can return to your main event loop periodically and then
come back and resume the next piece of work. That means your main
event loop becomes like this:
while (EvtGetEvent(& event, nonzerotimeout))
{
if (need to handle event)
{
handle it;
}
else
{
do a piece of work from your other loop;
}
}
In effect, you are creating a state machine and combining your loop
into the main event loop, so that they are together just one loop.
Another solution is to just call EvtGetPen() periodically inside
your loop. Then if you get a pendown, check if it is inside the
stop button's bounds, and if so, exit your loop. This works OK,
but it has a big limitation: EvtGetPen() only tells you whether
the pen is down at THAT instant. So, the user will have to HOLD
the pen down until you call EvtGetPen() for the stop button to
be pressed. If you can live with that limitation, this is by far
the easiest way to handle the situation.
Other solutions exist, but they are tricky. One is to keep your
own queue of events in addition to the system's queue. Then,
you can call EvtGetEvent() anywhere you want to to scan for events
(such as the pendownevent that indicates that somebody might be
pressing your stop button) -- if you don't care about the event,
you can just put it into your own queue and keep going, and the
main event loop can check your queue before the system queue.
But, that's pretty complex considering what little extra capability
it buys you. There are other solutions too, like moving the body
of your main event loop into a function -- then if you are careful,
you can call EvtGetEvent() and pass that event to that function
at almost any place in your program. BUT, if you do that, you need
to be careful since you are processing a new event before you are
finished with the current one. If you're not careful, you could
have stack overflow easily that way as well as other problems.
- Logan
--
For information on using the PalmSource Developer Forums, or to unsubscribe,
please see http://www.palmos.com/dev/support/forums/