|
Post by rodsweb on Jan 16, 2022 8:14:26 GMT -5
HowDoWeStop.bas (297 B) Hopefully there is a simple solution to this. How can I stop an LB programme in mid-run. I have a programme that processes data for hours. The useful stuff is saved to disk file. I need to be able to stop it at any point during the run, look at the mainwin data, and then continue. When in full flight LB ignores the x close window or "kill" instruction. I have a rather inelegant part solution by checking for the existence of a file called "stop" on the hard disk, if it exists the programme stops at an input instruction. So to stop the programme I have to write a file called "stop". This also means that the programme will only stop at the particular point that has the check. Surely there is a better solution that I have missed? See attached code for example. "n=40 for i=1 to 20 'once this starts I cannot stop it until it finishes the loops print "Have done ";i;" cycles of ";n;" will not stop until finished loops" for k=1 to 500000 'this loop simulates the real programmes calculations, but seconds long not hours next next print "Finished" end"
|
|
|
Post by Rod on Jan 16, 2022 8:25:10 GMT -5
You need to set up an interrupt event say a pause button, then in your loop place a scan statement. Then what happens is every time through the loop the program checks if the button has been clicked at the scan statement. If so it jumps to the buttons event handler. The mainwin that you are using is really for debugging and testing, you should have a gui window controlling what goes on because it can react to interrupt events.
Someone else may have a clever way of interrupting a mainwin loop. You can do a Ctrl Break to stop a loop but you want to pause and restart.
If I get a minute I will code a gui example unless someone beats me to it.
|
|
|
Post by Rod on Jan 16, 2022 8:30:17 GMT -5
Actually I have posted about this already.
nomainwin WindowWidth = 400 WindowHeight = 400 UpperLeftX = int((DisplayWidth-WindowWidth)/2) UpperLeftY = int((DisplayHeight-WindowHeight)/2) button #1.bp, "Pause", [pause], UL, 100, 100, 200,20 button #1.br, "Resume", [resume], UL, 100, 130,200, 20 textbox #1.tb 100,160,200,20 open "Pogram Manager" for window as #1 print #1, "trapclose [quit]" processing=1
[loop] scan if processing = 0 then wait count=count+1 #1.tb count 'your code here goto [loop]
[pause] processing=0 goto [loop]
[resume] processing=1 goto [loop]
|
|
|
Post by Walt Decker on Jan 16, 2022 8:41:32 GMT -5
I would not do a scan on every iteration. Something like:
IF Count MOD 5 = 0 THEN SCAN END IF
|
|
|
Post by Rod on Jan 16, 2022 8:53:26 GMT -5
Not sure that is good advice since it consistently takes longer than a simple scan. 750ms seconds longer on my PC.
start=time$("ms") for n= 1 to 10000 count=count+1 scan next e=time$("ms") s=e-t print "Scan took ";s;" milliseconds"
start=time$("ms") for n= 1 to 10000 count=count+1 IF count MOD 5 = 0 THEN SCAN END IF next e=time$("ms") ms=e-t print "Mod Scan took ";ms;" milliseconds" print "Mod Scan is ";ms-s;" milliseconds longer"
|
|
|
Post by Walt Decker on Jan 16, 2022 8:58:21 GMT -5
In a 10000 count loop the scan could be triggered every 100 to 1000 iterations. I just use 5 as an example. It all depends on the loop.
|
|
|
Post by rodsweb on Jan 16, 2022 9:02:31 GMT -5
Thank you Rod that works. For completeness your code needs a [quit] label. Also the window for my data to display appears to need to be a "text" window.
Does the scan instruction cause a check on every code instruction? i.e. a slight delay on every code instruction?
Thanks again.
|
|
|
Post by Rod on Jan 16, 2022 10:50:11 GMT -5
You can easily check by running a loop without scan or the if then mod check. walt Walt it isn’t the scan that is the problem it is the overhead of the if then check that happens EVERY loop.
|
|
|
Post by Walt Decker on Jan 16, 2022 11:39:49 GMT -5
No. SCAN interrupts your code stream to check for events. You mention that it takes hours for your app to finish. That means one of two things, you have tons of data to process or you have tons of data to process and your code design is inefficient for processing the data. The SCAN should go where the code takes the most time, not just at the beginning or end of a loop.
Rod, a do nothing loop is not an effective test.
|
|
|
Post by Rod on Jan 16, 2022 11:55:19 GMT -5
I think you will find it is a very effective test and that the if then overhead is more than the scan overhead on its own.
Of course the loop will take longer with real code but the difference between scan and mod scan will remain.
I completely agree with the technique to limit intrusion into a loop, unfortunately on this occasion the scan on its own is much faster than the conditional scan. It is the checking of the condition that defeats us on this occasion because it has to happen every loop.
|
|
|
Post by Rod on Jan 16, 2022 15:33:22 GMT -5
rodsweb. The scan is only checked when it is found in the code. So all of the other statements process without delay. Only when it comes across scan does it check the event queue. So a tiny single extra check in each loop.
|
|
|
Post by rodsweb on Jan 16, 2022 17:47:15 GMT -5
Thanks Walt and Rod. I don't know about the programme efficiency. It is reading about 5000 files each containing about 5000 items, all read 300 ish times since I am concerned that there may not be sufficient memory to store the 25,000,000 data items. The data is processsed into about 300 files containing 23,000 lines. However I leave the computer running overnight, so time is not much of a problem. The main reason for wanting to stop processing is mainly for debugging.
|
|
|
Post by Brandon Parker on Jan 18, 2022 19:55:48 GMT -5
rodsweb. The scan is only checked when it is found in the code. So all of the other statements process without delay. Only when it comes across scan does it check the event queue. So a tiny single extra check in each loop. Yeah, what Rod is saying is that if you have a process that is looping through thousands of records and takes time to do so for whatever reason, you should use a Scan command in that loop. If a program branches from a Wait statement, and does not return until the massively long routine is complete, all queued messages will just be waiting. This is exactly what the Scan statement is for. So, I am with Rod on this one. {:0) Brandon Parker
|
|