Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Timer Trap Problem
#1
Hi,

This code does not work:

Code: (Select All)

Rem sample of timer return
t1 = _FreeTimer
On Timer(t1, 1) GoSub Trap
Timer(t1) On
Start:
Do
  x$ = InKey$
  If x$ = Chr$(27) Then End
Loop
End
Trap:
Count = Count + 1
Print "Trap count:"; Count
Return Start
Reply
#2
Simply return from your timer:

Code: (Select All)

Rem sample of timer return
t1 = _FreeTimer
On Timer(t1, 1) GoSub Trap
Timer(t1) On
Start:
Do
x$ = InKey$
If x$ = Chr$(27) Then End
Loop
End
Trap:
Count = Count + 1
Print "Trap count:"; Count
Return
Reply
#3
That obviously solves the code snippet I posted, however, the reason why I need it to return to a line label is because I
am writing a large program trapping control-break with _exit which prompts Quit/Resume and since the
do/loop has 100 lines I don't want to return to somewhere inside the loop, only to the top of the loop..

Erik.

is what I just said more clear?? Huh
Reply
#4
Erik, don't take this the wrong way, but that's about the worst idea I think I've ever heard of from a programmer.

On Timer can occur at ANY point inside your code.  

You could have executed 10 lines of your code and went to the On Timer.
You could have executed 40 lines of your code and went to the On Timer.  
As for you next 60 lines of code?  PAH!!  You may as well cut them out of the damn program, as your On Timer triggers and stops execution from ever getting to them.  Oh, maybe not on YOUR PC, but on Pete's old slow one?  Or how about if your PC has a heavy workload and is running slow?  

You don't know where the heck that event was called from, or how deep into your program things might run before it triggers.  

The idea of On Timer is "skip out, do something, skip back..."   

What you're suggesting is "skip out, do something, go someplace completely unrelated".

If that's what you're wanting to do, set up your code to do it with a flag or something:

Code: (Select All)
Rem sample of timer return
t1 = _FreeTimer
On Timer(t1, .1) GoSub Trap
Timer(t1) On
Start:
Do
    x$ = InKey$
    If x$ = Chr$(27) Then End
    If start Then start = 0: GoTo Start 'copy this line where ever the hell you need an exit back to start
Loop
End
Trap:
Count = Count + 1
Print "Trap count:"; Count
start = -1
Return

Return label just isn't designed to work like that with ON TIMER. and it never should be.
Reply
#5
Ok, thanks for the feedback. Figures the only way to solve the problem is to add a flag...

Which is what I am already doing with a ControlBreak flag. I guess it was never meant to be.

Erik.
Reply
#6
The actual code to trap Control-Break I am using is posted here:

Code: (Select All)

Rem ctrl-break sample v1.0a

' disable ctrl-break.
Print "Press Ctrl-Break to test:"
v = _Exit

' jump here to restart trap.
start:
Timer(t1) Off
t1 = _FreeTimer
Print "Timer handle"; t1
On Timer(t1, 1) GoSub trap
Timer(t1) On

' start inkey$ loop.
z = z + 1
Print "Timer start loop"; z
Do
  x$ = InKey$
  If Len(x$) Then
      If x$ = Chr$(27) Then End
  End If
Loop

' trap ctrl-break.
trap:
v = _Exit
y = y + 1
Print "Trap call"; y
If v Then
  x = x + 1
  Print "Break trap"; x
  Return start
End If
Return
Reply
#7
(10-09-2024, 03:37 AM)eoredson Wrote: The actual code to trap Control-Break I am using is posted here:

Code: (Select All)

Rem ctrl-break sample v1.0a

' disable ctrl-break.
Print "Press Ctrl-Break to test:"
v = _Exit

' jump here to restart trap.
start:
Timer(t1) Off
t1 = _FreeTimer
Print "Timer handle"; t1
On Timer(t1, 1) GoSub trap
Timer(t1) On

' start inkey$ loop.
z = z + 1
Print "Timer start loop"; z
Do
  x$ = InKey$
  If Len(x$) Then
      If x$ = Chr$(27) Then End
  End If
Loop

' trap ctrl-break.
trap:
v = _Exit
y = y + 1
Print "Trap call"; y
If v Then
  x = x + 1
  Print "Break trap"; x
  Return start
End If
Return
    That seems like a lot of code for nothing.   Once _exit is called once then Ctrl/Break and "click the X" is trapped.   _exit returns the counts.    I just check _exit in an ON TIMER and set a flag variable.    I check that flag variable in my main program loop and set the behavior as if a User had selected my normal program Exit option.    Often that involves asking "Really want to Quit Y/N "     I think it's pretty important especially if you want your program to clean up any temporary files it might have created and/or return to the directory it started in if you may have changed directory's within the program.
Reply
#8
Why not just throw an
Code: (Select All)
IF _EXIT THEN GOTO start
in the loop. 
Why use an ON TIMER to begin with?
Reply
#9
(10-09-2024, 03:42 PM)Cobalt Wrote: Why not just throw an
Code: (Select All)
IF _EXIT THEN GOTO start
in the loop. 
Why use an ON TIMER to begin with?

It's used in the example for _Exit in Wiki.
   
b = b + ...
Reply
#10
(10-09-2024, 03:42 PM)Cobalt Wrote: Why not just throw an
Code: (Select All)
IF _EXIT THEN GOTO start
in the loop. 
Why use an ON TIMER to begin with?
Because break checking is being done in dozens of Sub/Functions and they cannot goto a line label in the main body of code.

It's just easier to trap the break and/or use a flag..

btw: be careful because Ctrl-Break will also put Ctrl-C into Inkey$
Reply




Users browsing this thread: 1 Guest(s)