Page 1 of 1
Is there a timer I can use in a console application?
Posted: Sat Nov 02, 2019 9:19 pm
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
Re: Is there a timer I can use in a console application?
Posted: Sat Nov 02, 2019 10:26 pm
by Derren
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?
Posted: Sat Nov 02, 2019 11:41 pm
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
Re: Is there a timer I can use in a console application?
Posted: Sun Nov 03, 2019 12:51 pm
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
Re: Is there a timer I can use in a console application?
Posted: Sun Nov 03, 2019 1:39 pm
by AAT
Re: Is there a timer I can use in a console application?
Posted: Tue Nov 05, 2019 11:44 pm
by Kurzer
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