PDA

View Full Version : only two of four timer (counters) are working



largo_winch
06-03-2013, 20:11
perhaps this can be a problem or a bug in current thinbasic issue:


' Empty GUI script created on 03-06-2013 05:48:22 by (thinAir)


Uses "UI", "console"' -- ID numbers of controls
Begin ControlID
%lTimer1
%lTimer2
%lTimer3
%lTimer4
%Timer1
%Timer2
%Timer3
%Timer4
End ControlID


Begin Const
%MAIN_WIDTH = 320
%MAIN_HEIGHT = 240
%Time = 1
%SleepTime = 500
End Const
Global Counter1 As DWord
Global Counter2 As DWord
Global Counter3 As DWord
Global Counter4 As DWord

' -- Create dialog here
Function TBMain()
Local hDlg As DWord


Dialog New Pixels, 0, "only 2 of 4 timer are working!",-1,-1, %MAIN_WIDTH, %MAIN_HEIGHT, _
%WS_POPUP Or %WS_VISIBLE Or %WS_CAPTION Or %WS_SYSMENU Or %WS_MINIMIZEBOX To hDlg
Dialog Show Modal hDlg, Call cbDialog


End Function


' -- Callback for dialog
CallBack Function cbDialog()


' -- Test for messages
Select Case CBMSG


Case %WM_INITDIALOG
' -- Place controls here
Control Add Label, CBHNDL, %lTimer1, "?", 10,10, 100, 25
Dialog Set Timer CBHNDL, %Timer1 , %Time

Control Add Label, CBHNDL, %lTimer2, "?", 10,40, 100, 25
Dialog Set Timer CBHNDL, %Timer2 , %Time

Control Add Label, CBHNDL, %lTimer3, "", 10,80, 100, 25
Dialog Set Timer CBHNDL, %Timer3 , %Time

Control Add Label, CBHNDL, %lTimer4, "", 10,120, 100, 25
Dialog Set Timer CBHNDL, %Timer4 , %Time

Case %WM_TIMER
Select Case CBCTL

Case %Timer1
Dialog Kill Timer CBHNDL, %Timer1
Incr Counter1
Sleep %SleepTime
Control Set Text CBHNDL,%lTimer1,Counter1
Dialog Set Timer CBHNDL, %Timer1, %Time

Case %Timer2
Dialog Kill Timer CBHNDL, %Timer2
Incr counter2
Sleep %SleepTime
Control Set Text CBHNDL,%lTimer2,Counter2
Dialog Set Timer CBHNDL, %Timer2, %Time

Case %Timer3
Dialog Kill Timer CBHNDL, %Timer3
Incr Counter3
Sleep %SleepTime
Control Set Text CBHNDL,%lTimer3,Counter3
Dialog Set Timer CBHNDL, %Timer3, %Time

Case %Timer4
Dialog Kill Timer CBHNDL, %Timer4
Incr Counter4
Sleep %SleepTime
Control Set Text CBHNDL,%lTimer4,Counter4
Dialog Set Timer CBHNDL, %Timer4, %Time

End Select


Case %WM_CLOSE
Dialog Kill Timer CBHNDL, %Timer1
Dialog Kill Timer CBHNDL, %Timer2
Dialog Kill Timer CBHNDL, %Timer3
Dialog Kill Timer CBHNDL, %Timer4
Dialog End CBHNDL


End Select
End Function



notice: tested under: win xp service pack 3

linked info from:
http://www.thinbasic.com/community/showthread.php?11977-Counter&p=88117#post88117

bye, largo

Petr Schreiber
06-03-2013, 20:18
Hi Largo_Winch,

on my PC and with thinBasic 1.9.3.0 everything works correctly - all 4 labels get updated.


Petr

largo_winch
07-03-2013, 11:27
thanks for testing, petr, but if you are killing the timer ('Dialog Kill Timer CBHNDL, %Timer1..), how that can run? that's a mistery for me

only this can work for me: I have desactivated in callback function below %WM_TIMER all


'Dialog Kill Timer CBHNDL, %Timer1

and all timers are working now.


' Empty GUI script created on 03-07-2013 05:48:22 by (thinAir)


Uses "UI", "console"' -- ID numbers of controls
Begin ControlID
%lTimer1
%lTimer2
%lTimer3
%lTimer4
%lTimer5
%Timer1
%Timer2
%Timer3
%Timer4
%Timer5
%Timer6
End ControlID


Begin Const
%MAIN_WIDTH = 320
%MAIN_HEIGHT = 240
%Time = 1
%SleepTime = 500
End Const
Global Counter1 As DWord
Global Counter2 As DWord
Global Counter3 As DWord
Global Counter4 As DWord
Global Counter5 As DWord

' -- Create dialog here
Function TBMain()
Local hDlg As DWord


Dialog New Pixels, 0, "all 4 timer(s) are working!",-1,-1, %MAIN_WIDTH, %MAIN_HEIGHT, _
%WS_POPUP Or %WS_VISIBLE Or %WS_CAPTION Or %WS_SYSMENU Or %WS_MINIMIZEBOX To hDlg
Dialog Show Modal hDlg, Call cbDialog


End Function


' -- Callback for dialog
CallBack Function cbDialog()


' -- Test for messages
Select Case CBMSG


Case %WM_INITDIALOG
' -- Place controls here
Control Add Label, CBHNDL, %lTimer1, "a", 10,10, 100, 25
Dialog Set Timer CBHNDL, %Timer1 , %Time

Control Add Label, CBHNDL, %lTimer2, "b", 10,40, 100, 25
Dialog Set Timer CBHNDL, %Timer2 , %Time

Control Add Label, CBHNDL, %lTimer3, "c", 10,80, 100, 25
Dialog Set Timer CBHNDL, %Timer3 , %Time

Control Add Label, CBHNDL, %lTimer4, "d", 10,120, 100, 25
Dialog Set Timer CBHNDL, %Timer4 , %Time

Control Add Label, CBHNDL, %lTimer5, "e", 10,120, 100, 25
Dialog Set Timer CBHNDL, %Timer5 , %Time

Case %WM_TIMER
Select Case CBCTL

Case %Timer1
'Dialog Kill Timer CBHNDL, %Timer1 ' that you needed for correct timer running?

Incr Counter1
Sleep %SleepTime
Control Set Text CBHNDL,%lTimer1,Counter1
'PrintL "counter1: " + Str$(Counter1)
Dialog Set Timer CBHNDL, %Timer1, %Time


Case %Timer2
'Dialog Kill Timer CBHNDL, %Timer2
Incr counter2
Sleep %SleepTime
Control Set Text CBHNDL,%lTimer2,Counter2
Dialog Set Timer CBHNDL, %Timer2, %Time

Case %Timer3
'Dialog Kill Timer CBHNDL, %Timer3
Incr Counter3
Sleep %SleepTime
Control Set Text CBHNDL,%lTimer3,Counter3
Dialog Set Timer CBHNDL, %Timer3, %Time

Case %Timer4
'Dialog Kill Timer CBHNDL, %Timer4
Incr Counter4
Sleep %SleepTime
Control Set Text CBHNDL,%lTimer4,Counter4
Dialog Set Timer CBHNDL, %Timer4, %Time

Case %Timer5
'Dialog Kill Timer CBHNDL, %Timer5
Incr Counter5
Sleep %SleepTime
Control Set Text CBHNDL,%lTimer5,Counter5
Dialog Set Timer CBHNDL, %Timer5, %Time

End Select


Case %WM_CLOSE
Dialog Kill Timer CBHNDL, %Timer1
Dialog Kill Timer CBHNDL, %Timer2
Dialog Kill Timer CBHNDL, %Timer3
Dialog Kill Timer CBHNDL, %Timer4
Dialog End CBHNDL


End Select
End Function




'WaitKey



bye, largo

largo_winch
07-03-2013, 11:31
powerbasic example (with a big help from my uncle frank!) as a proof works with correct killing timer below %WM_TIMER for three times:


'Compilable Example:
#COMPILE EXE
#DIM ALL
%Unicode=1
#INCLUDE "Win32API.inc"
%ID_Timer = 200
%ID_Timer2 = 201
%ID_Timer3 = 202


%bID_Timer = 301
%bID_Timer2 = 302
%bID_Timer3 = 303


%Time = 1
%SleepTime = 500


%pause = 600
%continue = 601
%graphic = 602


GLOBAL hDlg AS DWORD, Continue AS LONG
GLOBAL counter1 AS LONG, counter2 AS LONG,counter3 AS LONG


FUNCTION PBMAIN () AS LONG
DIALOG NEW PIXELS, 0, "Box Animation+Timers",,, 360,300, %WS_OVERLAPPEDWINDOW TO hDlg
CONTROL ADD BUTTON, hDlg, %pause, "Pause", 140,10,70,20
CONTROL ADD BUTTON, hDlg, %continue, "Continue", 230,10,70,20
CONTROL ADD GRAPHIC, hDlg, %graphic, "", 10,40,340,250, %WS_BORDER

GRAPHIC ATTACH hDlg, %graphic
DIALOG SHOW MODAL hDlg, CALL DlgProc
END FUNCTION


CALLBACK FUNCTION DlgProc() AS LONG
SELECT CASE CB.MSG
CASE %WM_INITDIALOG
SetTimer(hDlg, %ID_Timer, %pause, BYVAL %NULL) : Continue = 1
CONTROL ADD LABEL, hDlg, %bID_Timer, "", 10,10, 20, 25
CONTROL ADD LABEL, hDlg, %bID_Timer2, "", 40,10, 20, 25
CONTROL ADD LABEL, hDlg, %bID_Timer3, "", 80,10, 20, 25
'SetTimer(hDlg, %ID_Timer2, %pause, BYVAL %NULL) : Continue = 1
CASE %WM_COMMAND
SELECT CASE CB.CTL
CASE %pause : Continue = 0

CASE %continue : Continue = 1
END SELECT
CASE %WM_TIMER


KillTimer CBHNDL, %ID_Timer
INCR Counter1
SLEEP %SleepTime
CONTROL SET TEXT CBHNDL,%bID_Timer,STR$(Counter1)
SetTimer( CBHNDL, %ID_TIMER, %Time)


KillTimer CBHNDL, %ID_Timer2
INCR Counter2
SLEEP %SleepTime
CONTROL SET TEXT CBHNDL,%bID_Timer2,STR$(Counter2)
SetTimer( CBHNDL, %ID_TIMER2, %Time)


KillTimer CBHNDL, %ID_Timer3
INCR Counter3
SLEEP %SleepTime
CONTROL SET TEXT CBHNDL,%bID_Timer3,STR$(Counter3)
SetTimer( CBHNDL, %ID_TIMER3, %Time)


IF Continue THEN MoveBox
DIALOG SET TEXT hDlg, "Box Animation " + TIME$
CASE %WM_DESTROY
KillTimer CB.HNDL, %ID_Timer
KillTimer CB.HNDL, %ID_Timer2
KillTimer CB.HNDL, %ID_Timer3
END SELECT
END FUNCTION


SUB MoveBox
LOCAL x,y AS LONG
GRAPHIC CLEAR
x = RND(40,300) : y = RND(40,210)
GRAPHIC BOX (x,y)-(x+30,y+30),,%BLUE,%GREEN
END SUB




bye, largo

ReneMiner
07-03-2013, 11:57
I know it's not thinBasic, just something I experienced with it, in your code (lines 49++) I read this:
(Case followed by colon and another statement on one line)



' ...
SELECT CASE CB.CTL
CASE %pause : Continue = 0

CASE %continue : Continue = 1
END SELECT
' ...

which sometimes does not work (http://www.thinbasic.com/community/project.php?issueid=372&filter=all#note2324) in thinBasic. If you port this script do not write "Case Expression : Statement" on one line to be on the safe side. Otherwise the variable "Continue" might never change its value.

Petr Schreiber
07-03-2013, 16:02
Hi Largo_Winch,



thanks for testing, petr, but if you are killing the timer ('Dialog Kill Timer CBHNDL, %Timer1..), how that can run? that's a mistery for me


There is no mystery involved. Timer causes the dialog to receive WM_TIMER event, with timer ID passed in CBCTL parameter.

You never know, how long the code in the handling of WM_TIMER will execute, so it is good to disable the timer on the start (KILL) and restart it on the end (SET). It is perfectly legal, it just prevents firing another tick of the timer in the meantime.


Petr

largo_winch
08-03-2013, 18:17
ok thanks petr, but my problem is still the same as in my first post:
if I use "Dialog Kill Timer CBHNDL, %Timer1" in %WM_TIMER section my example doesn't run correct. If I am desactivating this line everything is working. but original (like powerbasic example shows this example all timers are working on my machine very fine !) I have to work with "Dialog Kill Timer CBHNDL, %TIMER1". can you provide me with your running solution and show it for me at the board? I suppose it's a software "timer" bug under win xp not a hardware problem.

bye, largo

Michael Clease
08-03-2013, 22:56
It would seem it doesn't matter how many times I tell people to NOT use the sleep command inside ui they continue to ignore the fact windows will continue to send timer events to the message queue while the thinbasic is asleep.

largo_winch
09-03-2013, 11:42
mike, this example it's taken from your own post here! from this example I've place sleep and "dialog kill timer" infos for my example ! so you have to correct yourself and fixed the example, see info in my first post ;)

linked info from:
http://www.thinbasic.com/community/s...8117#post88117 (http://www.thinbasic.com/community/showthread.php?11977-Counter&p=88117#post88117)


Try this code and see what results you get it shows an important thing about timers and why you should kill the timer while processing it. Play with %SleepTime to see what happens.



Uses "UI"' -- ID numbers of controls
Begin ControlID
%lTimer1
%lTimer2
%lTimer3
%Timer1
%Timer2
%Timer3
End ControlID


Begin Const
%MAIN_WIDTH = 320
%MAIN_HEIGHT = 240
%Time = 1
%SleepTime = 500
End Const
Global Counter1 As DWord
Global Counter2 As DWord
Global Counter3 As DWord


' -- Create dialog here
Function TBMain()
Local hDlg As DWord


Dialog New Pixels, 0, "<Enter title here>",-1,-1, %MAIN_WIDTH, %MAIN_HEIGHT, _
%WS_POPUP Or %WS_VISIBLE Or %WS_CAPTION Or %WS_SYSMENU Or %WS_MINIMIZEBOX To hDlg
Dialog Show Modal hDlg, Call cbDialog


End Function


' -- Callback for dialog
CallBack Function cbDialog()


' -- Test for messages
Select Case CBMSG


Case %WM_INITDIALOG
' -- Place controls here
Control Add Label, CBHNDL, %lTimer1, "", 10,10, 100, 25
Dialog Set Timer CBHNDL, %Timer1 , %Time
Control Add Label, CBHNDL, %lTimer2, "", 10,40, 100, 25
Dialog Set Timer CBHNDL, %Timer2 , %Time
Control Add Label, CBHNDL, %lTimer3, "", 10,80, 100, 25
Dialog Set Timer CBHNDL, %Timer3 , %Time


Case %WM_TIMER


Select Case CBCTL

Case %Timer1
Dialog Kill Timer CBHNDL, %Timer1
Incr Counter1
Sleep %SleepTime
Control Set Text CBHNDL,%lTimer1,Counter1
Dialog Set Timer CBHNDL, %Timer1, %Time
Case %Timer2
Incr counter2
Sleep %SleepTime
Control Set Text CBHNDL,%lTimer2,Counter2
Case %Timer3
Dialog Kill Timer CBHNDL, %Timer3
Incr Counter3
Sleep %SleepTime
Control Set Text CBHNDL,%lTimer3,Counter3
Dialog Set Timer CBHNDL, %Timer3, %Time

End Select


Case %WM_CLOSE
Dialog Kill Timer CBHNDL, %Timer1
Dialog Kill Timer CBHNDL, %Timer2
Dialog Kill Timer CBHNDL, %Timer3
Dialog End CBHNDL


End Select


End Function





It would seem it doesn't matter how many times I tell people to NOT use the sleep command inside ui they continue to ignore the fact windows will continue to send timer events to the message queue while the thinbasic is asleep.

I can only learn about thinbasic example and timer stuff there's at the forum board or from ui/sample folders.

perhaps somebody can show correct timer example for my first post and question.

bye, largo

Michael Clease
09-03-2013, 20:23
If you read the whole thread you can see I was explaining why you shouldn't use sleep and said


This should result in an extra 150ish calls while you are asleep


Try this code and see what results you get it shows an important thing about timers and why you should kill the timer while processing it. Play with %SleepTime to see what happens.


Perhaps I should put "don't use this script as it is an example of how not to do it."

bye mike