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

Just starting out? Need help? Post your questions and find answers here.
wimapon
Enthusiast
Enthusiast
Posts: 290
Joined: Thu Dec 16, 2010 2:05 pm
Location: Delfzijl ( The Netherlands )
Contact:

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

Post by wimapon »

How can i make a repeat ---- until loop which stops when i press the "s" key
epidemicz
User
User
Posts: 86
Joined: Thu Jan 22, 2009 8:05 am
Location: USA
Contact:

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

Post 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

Last edited by epidemicz on Fri Jan 07, 2011 10:11 pm, edited 2 times in total.
Image
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

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

Post by blueznl »

AddKeyboardShortcut() ?
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
wimapon
Enthusiast
Enthusiast
Posts: 290
Joined: Thu Dec 16, 2010 2:05 pm
Location: Delfzijl ( The Netherlands )
Contact:

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

Post by wimapon »

thanks epidemicz..
that's it
Wim
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

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

Post 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
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
wimapon
Enthusiast
Enthusiast
Posts: 290
Joined: Thu Dec 16, 2010 2:05 pm
Location: Delfzijl ( The Netherlands )
Contact:

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

Post 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
wimapon
Enthusiast
Enthusiast
Posts: 290
Joined: Thu Dec 16, 2010 2:05 pm
Location: Delfzijl ( The Netherlands )
Contact:

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

Post by wimapon »

ts_soft
I did merge your program with mine...
but no result....
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

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

Post by IdeasVacuum »

If it must halt the 'whole program', then the loop must encompass the whole program.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
wimapon
Enthusiast
Enthusiast
Posts: 290
Joined: Thu Dec 16, 2010 2:05 pm
Location: Delfzijl ( The Netherlands )
Contact:

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

Post by wimapon »

it does...but it does not stop when i press a key
c4s
Addict
Addict
Posts: 1981
Joined: Thu Nov 01, 2007 5:37 pm
Location: Germany

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

Post by c4s »

The loop is "blocked" because of the calculations? I'd say you would need a thread then.
If any of you native English speakers have any suggestions for the above text, please let me know (via PM). Thanks!
wimapon
Enthusiast
Enthusiast
Posts: 290
Joined: Thu Dec 16, 2010 2:05 pm
Location: Delfzijl ( The Netherlands )
Contact:

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

Post 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
User avatar
KJ67
Enthusiast
Enthusiast
Posts: 218
Joined: Fri Jun 26, 2009 3:51 pm
Location: Westernmost tip of Norway

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

Post 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.
The best preparation for tomorrow is doing your best today.
wimapon
Enthusiast
Enthusiast
Posts: 290
Joined: Thu Dec 16, 2010 2:05 pm
Location: Delfzijl ( The Netherlands )
Contact:

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

Post by wimapon »

Yes, but i can not program that...
heinz
New User
New User
Posts: 3
Joined: Sat Jan 08, 2011 10:15 am

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

Post 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
User avatar
KJ67
Enthusiast
Enthusiast
Posts: 218
Joined: Fri Jun 26, 2009 3:51 pm
Location: Westernmost tip of Norway

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

Post 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
The best preparation for tomorrow is doing your best today.
Post Reply