IonicWind Software

IWBasic => General Questions => Topic started by: RitchieF on May 30, 2009, 10:31:01 AM

Title: Break a For-Next-Loop
Post by: RitchieF on May 30, 2009, 10:31:01 AM
Hi Friends,

I have written a small cnc-related tool, which is parsing every line of a cnc-program and doing some convertions if necessary .

The conversion-code in Ebasic is in a subroutine and works pretty well. The problem is :  the cnc-program can be very long and the user has to wait a long time til the nc-code is cionverted and my tool reacts to user-input again.

So I added a 'Break the conversion'-button to break the For-Next-Loop within the subroutine. I installed a handler subroutine to process a message from this button.

In the message handler subroutine I have just one command: breakfor. But this only causes the message : breakfor outside for-next-loop.

So my question is : where must I add the button-related code to break the for-next-loop inside the subroutine and is breakfor the right way to do this ?

Thanks in advance,

Richard
Title: Re: Break a For-Next-Loop
Post by: Ionic Wind Support Team on May 30, 2009, 10:43:29 AM
Richard

BREAKFOR must be used in between a FOR and NEXT statement, or the compiler wouldn't know what FOR/NEXT you were trying to break.

You have a number of ways of handling the problem.  The simplest is to use WAIT 1 within the FOR/NEXT loop and check for an exit variable.

FOR x = 1 to longtime
....
WAIT 1
IF exit_longtime = TRUE
   BREAKFOR
ENDIF
...
NEXT x

Then in your handler subroutine you only need to do:

exit_longtime = TRUE

This will serve two purposes, it will leave your GUI responsive to user input, and it will allow you to break out of the FOR loop.  I mentioned it is the simplest way to do it, there are more complex methods, such as putting your conversion code in a thread, and checking for an exit condition in the main process.  But I leave that as an exercise if you are interested.  There is code on the forums that show how to do multithreading. Just search for "CreateThread"

Paul.

Title: Re: Break a For-Next-Loop
Post by: RitchieF on May 31, 2009, 06:52:07 AM
Paul,

thanks for your help. Works fine for me here !

Quoteputting your conversion code in a thread

Sounds good, but I don't want to show my code, coz I have a competitor in a different forum with the same tool written in Delphi and I don't want to give that guy some help  ;)

As much as I can test up to now, Emergence runs faster than this Delphi - tool with the same nc-code to convert !!! ;D

You are doing great coding !!

Richard