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
Is there a timer I can use in a console application?
Is there a timer I can use in a console application?
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!"
"Happiness is a pet." | "Never run a changing system!"
Re: Is there a timer I can use in a console application?
If you have a "main program" that is a window, you can still use WindowTimer()
Re: Is there a timer I can use in a console application?
Console programs have not windows and gadgets. So not WindowTimer exists.
You can work over threads...
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
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Re: Is there a timer I can use in a console application?
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
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!"
"Happiness is a pet." | "Never run a changing system!"
Re: Is there a timer I can use in a console application?
I almost forgot. Thank you for your example mk-soft. 
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

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!"
"Happiness is a pet." | "Never run a changing system!"