Threaded time (No interference from other tasks)

Just starting out? Need help? Post your questions and find answers here.
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4792
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Threaded time (No interference from other tasks)

Post by Fangbeast »

I use the below code with Danilo's excellent high res timer library and it works great. It allows me to stop and start a message with every routine but it also gets interrupted by other tasks so that is a problem

Is there any way to have a timer task run independandtly of other tasks in a program such as in a thread to not get stopped or frozen?

Code: Select all

#WindowIndex                            = 0
#GadgetIndex                            = 0
#ImageIndex                             = 0
#StatusBarIndex                         = 0
#MenuBarIndex                           = 0

#Window_Form1                           = #WindowIndex : #WindowIndex = #WindowIndex + 1

#Gadget_Form1_String2                   = #GadgetIndex : #GadgetIndex = #GadgetIndex + 1
#Gadget_Form1_Button3                   = #GadgetIndex : #GadgetIndex = #GadgetIndex + 1

;==================================================================================================

Global TimerTicks.l

;==================================================================================================

Procedure.l Window_Form1()
  If OpenWindow(#Window_Form1,175,0,400,300,#PB_Window_SystemMenu|#PB_Window_Invisible,"Work Form1")
    If CreateGadgetList(WindowID())
      StringGadget(#Gadget_Form1_String2,10,270,300,20,"")
      ButtonGadget(#Gadget_Form1_Button3,325,270,60,20,"Button3")
      HideWindow(#Window_Form1,0)
      ProcedureReturn WindowID()
    EndIf
  EndIf
EndProcedure

;==================================================================================================

Procedure BlinkMessage()

  While WindowEvent() : Wend
  SetGadgetText(#Gadget_Form1_String2, "Working...")
  TimerTicks + 1
  If TimerTicks = 2
    TimerTicks = 0
    While WindowEvent() : Wend
    SetGadgetText(#Gadget_Form1_String2, "")
  EndIf

EndProcedure

;==================================================================================================

If Window_Form1()

  StartTimer(0, 1000, @BlinkMessage())
  
  quitForm1 = 0 

  Repeat
    EventID=WaitWindowEvent()
    Select EventID
      Case #PB_Event_CloseWindow
        If EventWindowID()=#Window_Form1
          quitForm1=1
        EndIf

      Case #PB_Event_Menu
        Select EventMenuID()
        EndSelect

      Case #PB_Event_Gadget
        Select EventGadgetID()
          Case #Gadget_Form1_String2
          Case #Gadget_Form1_Button3  : EndTimer(0)
        EndSelect

    EndSelect
  Until quitForm1
  CloseWindow(#Window_Form1)
EndIf
End
I embedded this in another program and one task in particular that generated crc's halted the timer completely till the crc for that file was done and that isn't good.
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet