Page 1 of 3

i like to stop a loop by pressing the "s" key

Posted: Fri Jan 07, 2011 9:43 pm
by wimapon
How can i make a repeat ---- until loop which stops when i press the "s" key

Re: i like to stop a loop by pressing the "s" key

Posted: Fri Jan 07, 2011 9:59 pm
by epidemicz
in a console application it could be done like this..

Code: Select all

openconsole()

repeat
key$=inkey()

debug "hello world"
until key$="s"
There's I'm sure a dozen ways to do a windowed example.. I'm not familiar with the normal way most people would do it with native pb commands so lol here's what Ive come up with.

PS: lol I have my own system using the win api to do this normally so dont pick on me if this is really terrible.

Code: Select all

;lol this is hacky 

OpenWindow(0, 0, 0, 240, 70, "ShortcutGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
AddKeyboardShortcut(0, #PB_Shortcut_S, 777)
Repeat
  Event = WaitWindowEvent()
  men=EventMenu()
  
Until Event = #PB_Event_CloseWindow Or men=777


Re: i like to stop a loop by pressing the "s" key

Posted: Fri Jan 07, 2011 10:06 pm
by blueznl
AddKeyboardShortcut() ?

Re: i like to stop a loop by pressing the "s" key

Posted: Fri Jan 07, 2011 10:11 pm
by wimapon
thanks epidemicz..
that's it
Wim

Re: i like to stop a loop by pressing the "s" key

Posted: Fri Jan 07, 2011 10:31 pm
by ts-soft
epidemicz wrote:

Code: Select all

;lol this is hacky 

OpenWindow(0, 0, 0, 240, 70, "ShortcutGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
AddKeyboardShortcut(0, #PB_Shortcut_S, 777)
Repeat
  Event = WaitWindowEvent()
  men=EventMenu()
  
Until Event = #PB_Event_CloseWindow Or men=777

this is incorrect code
corrected code:

Code: Select all

;lol this is hacky

OpenWindow(0, 0, 0, 240, 70, "ShortcutGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
AddKeyboardShortcut(0, #PB_Shortcut_S, 777)
Repeat
  Event = WaitWindowEvent()
  If Event = #PB_Event_Menu : men = EventMenu() : EndIf
 
Until Event = #PB_Event_CloseWindow Or men = 77

Re: i like to stop a loop by pressing the "s" key

Posted: Fri Jan 07, 2011 11:06 pm
by wimapon
But... i have a problem:

the repeat----until loop calculates for a minute.....and then calculates again
the stopping command "s" or whatever must be seen everywhere in the program...

ts-soft.. will your demo do that job?

wim

Re: i like to stop a loop by pressing the "s" key

Posted: Fri Jan 07, 2011 11:22 pm
by wimapon
ts_soft
I did merge your program with mine...
but no result....

Re: i like to stop a loop by pressing the "s" key

Posted: Fri Jan 07, 2011 11:41 pm
by IdeasVacuum
If it must halt the 'whole program', then the loop must encompass the whole program.

Re: i like to stop a loop by pressing the "s" key

Posted: Fri Jan 07, 2011 11:54 pm
by wimapon
it does...but it does not stop when i press a key

Re: i like to stop a loop by pressing the "s" key

Posted: Sat Jan 08, 2011 12:02 am
by c4s
The loop is "blocked" because of the calculations? I'd say you would need a thread then.

Re: i like to stop a loop by pressing the "s" key

Posted: Sat Jan 08, 2011 9:21 am
by wimapon
Yes, i think that it must be something like that.
If i put a inkey statement in the middel of the most uses loop in the calculation
it does not react either.

So... ... what to do now?

we are talking about the program that will measure the whole day, the signals
coming from the stars... via the soundcard.
It waits until there is a new minute, then measures 1 second, and than wait until
the next minute.
in that waiting loop i put the inkey statement....
the program does not need to stop ... but as usual.. once in the week in case of
problems, you must stop it by hand....

Wim

Re: i like to stop a loop by pressing the "s" key

Posted: Sat Jan 08, 2011 9:53 am
by KJ67
Mayby it's SysTray's you are looking for?

Then you could just have a small icon in the corner for your Logger/Recorder and when you get a "Quit" from the user you just shut down your program in a controlled way.

Re: i like to stop a loop by pressing the "s" key

Posted: Sat Jan 08, 2011 9:54 am
by wimapon
Yes, but i can not program that...

Re: i like to stop a loop by pressing the "s" key

Posted: Sat Jan 08, 2011 10:37 am
by heinz
Do not use waiting loops, use timer events instead.
One Main loop which starts the measurement when a timer event occurs:

OpenWindow(0, 0, 0, 240, 70, "ShortcutGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
AddKeyboardShortcut(0, #PB_Shortcut_S, 777)
AddWindowTimer(0, 1, 60000)

Repeat
Event = WaitWindowEvent()
If Event = #PB_Event_Menu
men = EventMenu()
ElseIf Event = #PB_Event_Timer And EventTimer() = 1
StartMeasurement1sec()
Endif
Until Event = #PB_Event_CloseWindow Or men = 777

Re: i like to stop a loop by pressing the "s" key

Posted: Sat Jan 08, 2011 11:06 am
by KJ67
Got a little carried away....
Maybe this skeleton can be something for you to implement?

Code: Select all

EnableExplicit

Declare Main()

main()  ; Start Main() Prcedure
End     ; And close all when shutting down....
;//////////////////////////


Procedure Sampler(MySemaphore)
  If TrySemaphore(MySemaphore)
    ;
    ;- *** Sampling code
    ;
    ;- *** File access code
    ;
    SignalSemaphore(MySemaphore) 
    ; Signal the semapore, this so that next thread can pick it up!
  EndIf
EndProcedure


Macro WaitAndBreak()
  ForEach MyThread()
    WaitThread(MyThread())
  Next
  Break
EndMacro


Procedure main()
  Protected hWind, Icon, i, Semaphore
  NewMap MyThread()
  
  Semaphore = CreateSemaphore(1)
  ; This could just as easely be a Mutex.
  ; It is just to secure that only one Thread rus ath the time, 
  ; and then only one thread writes to the log-file.
  
  hWind= OpenWindow(0,0,0,200,100,"window",#PB_Window_SystemMenu|#PB_Window_Minimize)
  
  CreatePopupMenu(0)
  MenuItem(1,"Quit")
  MenuItem(2,"Info")
  
  UsePNGImageDecoder()
  Icon = LoadImage(#PB_Any, #PB_Compiler_Home + "Examples\Sources\Data\world.png")
  
  AddSysTrayIcon(0,hWind,ImageID(Icon))
  
  AddWindowTimer(0, 0, 60*1000)    ; a 60 sec timer (for the Sampler()).
  AddWindowTimer(0, 0, 5*10*1000)  ; a 5 minute timer ( for internal Garbage Collection).
  
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_Timer 
        Select EventTimer()
            
          Case 0 ; need to verify which - if more then one...
            i=CreateThread(@Sampler(), Semaphore)
            If i
              MyThread(Str(i))=i    ; save all started threadID's.
            EndIf
            
          Case 1 ; Just an examle of a simpe garbage collector
            ForEach MyThread()
              If Not IsThread(MyThread())
                DeleteMapElement(MyThread())
              EndIf
            Next
          Default ; Should not be reached!
        EndSelect
        
      Case #PB_Event_SysTray
        DisplayPopupMenu(0,hWind)
        
      Case #PB_Event_CloseWindow
        WaitAndBreak()
        
      Case #PB_Event_Menu
        Select EventMenu()
          Case 1
            WaitAndBreak()
          Case 2
            MessageRequester("Hi!", "I'm a program.")
        EndSelect
    EndSelect
  ForEver
EndProcedure