Is there a timer I can use in a console application?

Just starting out? Need help? Post your questions and find answers here.
User avatar
Kurzer
Enthusiast
Enthusiast
Posts: 676
Joined: Sun Jun 11, 2006 12:07 am
Location: Near Hamburg

Is there a timer I can use in a console application?

Post by Kurzer »

Hello fellows,

I wrote a console application.
There are long lasting processes running and I want to inform the user about the past time of the process.

Can I use a kind of timer in a console application that runs parallel to the main program and, for example, updates a time display every second?

Thanks in advance for hints.
Kurzer
PB 6.12 x64, OS: Win 11 24H2 x64, Desktopscaling: 150%, CPU: I7 12700 H, RAM: 32 GB, GPU: Intel(R) Iris(R) Xe Graphics | NVIDIA GeForce RTX 3070, User age in 2025: 57y
"Happiness is a pet." | "Never run a changing system!"
User avatar
Derren
Enthusiast
Enthusiast
Posts: 316
Joined: Sat Jul 23, 2011 1:13 am
Location: Germany

Re: Is there a timer I can use in a console application?

Post by Derren »

If you have a "main program" that is a window, you can still use WindowTimer()
User avatar
mk-soft
Always Here
Always Here
Posts: 6246
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Is there a timer I can use in a console application?

Post by mk-soft »

Console programs have not windows and gadgets. So not WindowTimer exists.
You can work over threads...

Code: Select all

;-TOP

CompilerIf Not #PB_Compiler_Thread
  CompilerError "Use Compiler Option ThreadSafe!"
CompilerEndIf

Structure udtThread
  ThreadID.i
  Signal.i
  Index.i
  Cnt.i
  Ready.i
EndStructure

Global Work.udtThread

Work\Signal = CreateSemaphore()

Procedure thWork(*Data.udtThread)
  Protected i
  
  Repeat
    If c % 100 = 0
      i + 1
      *Data\Index = i
      *Data\Cnt = c
      SignalSemaphore(*Data\Signal)
    EndIf
    c + 1
    Delay(10)
  Until i >= 10
  
  *Data\Ready = #True
  SignalSemaphore(*Data\Signal)
    
EndProcedure

If OpenConsole("Threads")
  
  Work\ThreadID = CreateThread(@thWork(), Work)
  
  Repeat
    WaitSemaphore(Work\Signal)
    PrintN("Index " + Work\Index + " Count " + Work\Cnt)
  Until Work\Ready
  
EndIf
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Kurzer
Enthusiast
Enthusiast
Posts: 676
Joined: Sun Jun 11, 2006 12:07 am
Location: Near Hamburg

Re: Is there a timer I can use in a console application?

Post by Kurzer »

Yes, I have no window in my application.
Thanks for the thread approach, mk-soft.

Now I am outdoor with my pet, but I'll give it a try this afternoon.

Kurzer

Sent via mobile phone
PB 6.12 x64, OS: Win 11 24H2 x64, Desktopscaling: 150%, CPU: I7 12700 H, RAM: 32 GB, GPU: Intel(R) Iris(R) Xe Graphics | NVIDIA GeForce RTX 3070, User age in 2025: 57y
"Happiness is a pet." | "Never run a changing system!"
AAT
Enthusiast
Enthusiast
Posts: 259
Joined: Sun Jun 15, 2008 3:13 am
Location: Russia

Re: Is there a timer I can use in a console application?

Post by AAT »

User avatar
Kurzer
Enthusiast
Enthusiast
Posts: 676
Joined: Sun Jun 11, 2006 12:07 am
Location: Near Hamburg

Re: Is there a timer I can use in a console application?

Post by Kurzer »

I almost forgot. Thank you for your example mk-soft. Image
I had solved it differently in the meantime, but thanks to your example I was also able to clarify an ambiguity in the german PB help for myself.

The german description for semaphores is not very plausible. I've never worked with semaphores on other platforms in the past and so I never really understood it's use.

The german Help::WaitSemaphore() says: "Decreases the internal counter of the semaphore by one, and blocks further thread execution if the counter falls below zero."
Help::WaitSemaphore(): "A blocked thread is continued as soon as another thread calls SignalSemaphore()".

I have never considered the actual main program as a thread, but always assumed that threads were meant that were only created using CreateThread(). I wasn't aware until today that the main program also stops at this command. Based on your example, I have now understood that this is also called a thread. I never stop learning. Thanks for that.

Kurzer
PB 6.12 x64, OS: Win 11 24H2 x64, Desktopscaling: 150%, CPU: I7 12700 H, RAM: 32 GB, GPU: Intel(R) Iris(R) Xe Graphics | NVIDIA GeForce RTX 3070, User age in 2025: 57y
"Happiness is a pet." | "Never run a changing system!"
Post Reply