FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Timer Hash Problem for Daniel
Posts: 2706
Joined: Fri Oct 07, 2005 01:50 PM
Timer Hash Problem for Daniel
Posted: Mon Nov 05, 2012 06:48 PM
Daniel

In a previous thread you gave me an example of how to create a timer with variables using Hashes which worked perfectly.

However, trying to adapt your code to my application, I am running into a problem .. The code appears to work, only the clock elapsed time does not seem to update. Also, The timer needs to be able to stop and start on demand and pick up where it leaves off.

It appears to have something with the Redefine Get oTime line...Here is my code and .rc .. I am sure there is a quick solution and I would appreciate your advice.

Rick Lipkin
Code (fw): Select all Collapse
// Testing elapsed time

#include "FiveWin.ch"

//-------------------
function Main()

local hDatas := {=>}
local hVar, hObj
Local nClicks
Local oBtnClock,cClockText,oTmr

Local oDlg
hDatas["var"] = {=>}
hDatas["obj"] = {=>}

hVar = hDatas["var"]
hObj = hDatas["obj"]

hVar["nSec"]  = 0
hVar["nMin"]  = 0
hVar["nHour"] = 0
hVar["nDay"]  = 0
*hVar["oTmr"]  = 0
hVar["cTime"] = "000:00:00:00"

oTmr := " "

cClockText := "Start"
nClicks    := 1

DEFINE DIALOG oDlg RESOURCE "WORKVIEW" TITLE "Test"

       REDEFINE GET hObj["oTime"] var hVar["cTime"] ID 184 of oDlg READONLY

       REDEFINE BTNBMP oBtnClock ID 180 PROMPT cClockText ;
                CENTER ;
                of oDlg ;
                ACTION( nClicks++,;
               _BuildTimer( oBtnClock,cClockText,@nClicks,hDatas,oDlg,@oTmr))

ACTIVATE DIALOG oDlg CENTERED

return nil

//-----------------
function _BuildTimer( oBtnClock,cClockText,nClicks,hDatas,oDlg,oTmr )

*local oTmr
local hVar := hDatas["var"]
local hObj := hDatas["obj"]

If nClicks = 2 // very first time
   cClockText := "Stop"
   oBtnClock:cCAPTION := cClockText
   oBtnClock:ReFresh()
Else

   If IsEven( nCLicks )
    *  msginfo( "Even")
      cClockText := "Start"
      oBtnClock:cCAPTION := cClockText
      oBtnClock:ReFresh()

      cClockText := "Stop"
      oBtnClock:cCAPTION := cClockText
      oBtnClock:ReFresh()

   Else
    *  msginfo( "Odd")
      cClockText := "Stop"
      oBtnClock:cCAPTION := cClockText
      oBtnClock:ReFresh()

     * msginfo( "Stopping timer" )
      oTmr:End()

      cClockText := "Start"
      oBtnClock:cCAPTION := cClockText
      oBtnClock:ReFresh()

      Return(nil)
   Endif

Endif

oBtnClock:ReFresh()

DEFINE TIMER oTmr of oDlg ;
  ACTION ( hVar["nSec"]++,;
         If( hVar["nSec"]  >=60,(hVar["nMin"]++,hVar["nSec"] := 0), ),;
         If( hVar["nMin"]  >=60,(hVar["nHour"]++,hVar["nMin"] := 0), ),;
         If( hVar["nHour"] >=24,(hVar["nDay"]++,hVar["nHour"] := 0), ),;
         hVar["cTime"] := strzero(hVar["nDay"],3)+":"+strzero(hVar["nHour"],2)+":"+strzero(hVar["nMin"],2)+":"+strzero(hVar["nSec"],2),;
        HEVAL( hObj, {| key, val | If( "oS"$key, val:refresh(), )} ) );
 INTERVAL 1000

ACTIVATE TIMER oTmr

return nil

//-------------------------
Func IsEven( nInt )

If empty( nInt )
   Return(.f.)
Endif

If ( nInt % 2 == 0 )
   Return(.t.)
Endif

Return(.f.)

// end


Code (fw): Select all Collapse
WORKVIEW DIALOG 12, 57, 169, 156
STYLE WS_POPUP | WS_CAPTION | WS_SYSMENU
FONT 6, "MS Sans Serif"
{
 CONTROL "Close", 140, "TBtnBmp", 32 | WS_CHILD | WS_VISIBLE | WS_TABSTOP, 102, 77, 41, 48
 CTEXT "Timer", 168, 5, 34, 27, 11, SS_CENTER | SS_NOPREFIX | WS_GROUP
 EDITTEXT 184, 5, 47, 49, 12, ES_AUTOHSCROLL | NOT WS_TABSTOP | WS_BORDER
 CONTROL "Timer", 180, "TBtnBmp", 32 | WS_CHILD | WS_VISIBLE | WS_TABSTOP, 59, 36, 35, 30
}
Posts: 44162
Joined: Thu Oct 06, 2005 05:47 PM
Re: Timer Hash Problem for Daniel
Posted: Mon Nov 05, 2012 08:58 PM
Rick,

With these changes it seems to work fine:

Code (fw): Select all Collapse
DEFINE TIMER oTmr of oDlg ;
  ACTION ( hVar["nSec"]++,;
         If( hVar["nSec"]  >=60,(hVar["nMin"]++,hVar["nSec"] := 0), ),;
         If( hVar["nMin"]  >=60,(hVar["nHour"]++,hVar["nMin"] := 0), ),;
         If( hVar["nHour"] >=24,(hVar["nDay"]++,hVar["nHour"] := 0), ),;
         hVar["cTime"] := strzero(hVar["nDay"],3)+":"+strzero(hVar["nHour"],2)+":"+strzero(hVar["nMin"],2)+":"+strzero(hVar["nSec"],2),;
        HEVAL( hObj, {| key, val | val:SetText( hVar["cTime"] ) } ) );
 INTERVAL 1000
regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 2706
Joined: Fri Oct 07, 2005 01:50 PM
Re: Timer Hash Problem for Daniel
Posted: Mon Nov 05, 2012 09:04 PM
Antonio

I accidentally found a solution .. by changing the get from
Code (fw): Select all Collapse
REDEFINE GET hObj["oTime"] var hVar["cTime"] ID 184 of oDlg READONLY

to
Code (fw): Select all Collapse
REDEFINE GET hObj["oSay1"] var hVar["cTime"] ID 184 of oDlg READONLY


It worked .. I have no clue why .. oSay1 is not defined anywhere in my program .. it was just an accident transposing Daniels original code into mine.

Any ideas why changing the object variable would make it work ?

Rick Lipkin
Posts: 2365
Joined: Wed Nov 02, 2005 11:46 PM
Re: Timer Hash Problem for Daniel
Posted: Mon Nov 05, 2012 09:27 PM

Hello Rick

the Antonio's solutions should be work fine

the problem withh teh code is

HEVAL( hObj, {| key, val | If( "oS"$key, val:refresh(), )} ) );

because this condition isn't successful "If( "oS"$key"

the Hash in you code is
hDatas["obj"]
hDatas["obj"]["oTime"]

the keys in hDatas["obj"] are "oTime"

you can change the condition "If( "oT"$key".... or use Antonio's solution

other way is create a new key to hash with to include the object to refresh

example

hDatas["obj_to_refgresh"] = {=>}
hObj_to_refresh = hDatas["obj_to_refgresh"]

REDEFINE GET hObj_to_refresh["oTime"] var hVar["cTime"] ID 184 of oDlg READONLY

inside Function _BuildTimer
hObj_to_refresh = hDatas["obj_to_refgresh"]

HEVAL( hObj_to_refresh, {| key, val | val:SetText( hVar["cTime"] ) } ) )

Posts: 2706
Joined: Fri Oct 07, 2005 01:50 PM
Re: Timer Hash Problem for Daniel
Posted: Mon Nov 05, 2012 09:34 PM
Daniel

For some reason Antonio's solution did not work either .. I substituted the timer changes ..

Code (fw): Select all Collapse
// Testing elapsed time

#include "FiveWin.ch"

//-------------------
function Main()

local hDatas := {=>}
local hVar, hObj
Local nClicks
Local oBtnClock,cClockText,oTmr

Local oDlg
hDatas["var"] = {=>}
hDatas["obj"] = {=>}

hVar = hDatas["var"]
hObj = hDatas["obj"]

hVar["nSec"]  = 0
hVar["nMin"]  = 0
hVar["nHour"] = 0
hVar["nDay"]  = 0
*hVar["oTmr"]  = 0
hVar["cTime"] = "000:00:00:00"

oTmr := " "

cClockText := "Start"
nClicks    := 1

DEFINE DIALOG oDlg RESOURCE "WORKVIEW" TITLE "Test"

       REDEFINE GET hObj["oTime"] var hVar["cTime"] ID 184 of oDlg READONLY

       REDEFINE BTNBMP oBtnClock ID 180 PROMPT cClockText ;
                CENTER ;
                of oDlg ;
                ACTION( nClicks++,;
               _BuildTimer( oBtnClock,cClockText,@nClicks,hDatas,oDlg,@oTmr))

ACTIVATE DIALOG oDlg CENTERED

return nil

//-----------------
function _BuildTimer( oBtnClock,cClockText,nClicks,hDatas,oDlg,oTmr )

*local oTmr
local hVar := hDatas["var"]
local hObj := hDatas["obj"]

If nClicks = 2 // very first time
   cClockText := "Stop"
   oBtnClock:cCAPTION := cClockText
   oBtnClock:ReFresh()
Else

   If IsEven( nCLicks )
    *  msginfo( "Even")
      cClockText := "Start"
      oBtnClock:cCAPTION := cClockText
      oBtnClock:ReFresh()

      cClockText := "Stop"
      oBtnClock:cCAPTION := cClockText
      oBtnClock:ReFresh()

   Else
    *  msginfo( "Odd")
      cClockText := "Stop"
      oBtnClock:cCAPTION := cClockText
      oBtnClock:ReFresh()

     * msginfo( "Stopping timer" )
      oTmr:End()

      cClockText := "Start"
      oBtnClock:cCAPTION := cClockText
      oBtnClock:ReFresh()

      Return(nil)
   Endif

Endif

oBtnClock:ReFresh()

DEFINE TIMER oTmr of oDlg ;
  ACTION ( hVar["nSec"]++,;
         If( hVar["nSec"]  >=60,(hVar["nMin"]++,hVar["nSec"] := 0), ),;
         If( hVar["nMin"]  >=60,(hVar["nHour"]++,hVar["nMin"] := 0), ),;
         If( hVar["nHour"] >=24,(hVar["nDay"]++,hVar["nHour"] := 0), ),;
         hVar["cTime"] := strzero(hVar["nDay"],3)+":"+strzero(hVar["nHour"],2)+":"+strzero(hVar["nMin"],2)+":"+strzero(hVar["nSec"],2),;
        HEVAL( hObj, {| key, val | val:SetText( hVar["cTime"] ) } ) );
 INTERVAL 1000

/*
DEFINE TIMER oTmr of oDlg ;
  ACTION ( hVar["nSec"]++,;
         If( hVar["nSec"]  >=60,(hVar["nMin"]++,hVar["nSec"] := 0), ),;
         If( hVar["nMin"]  >=60,(hVar["nHour"]++,hVar["nMin"] := 0), ),;
         If( hVar["nHour"] >=24,(hVar["nDay"]++,hVar["nHour"] := 0), ),;
         hVar["cTime"] := strzero(hVar["nDay"],3)+":"+strzero(hVar["nHour"],2)+":"+strzero(hVar["nMin"],2)+":"+strzero(hVar["nSec"],2),;
        HEVAL( hObj, {| key, val | If( "oS"$key, val:refresh(), )} ) );
 INTERVAL 1000

ACTIVATE TIMER oTmr
*/
return nil

//-------------------------
Func IsEven( nInt )

If empty( nInt )
   Return(.f.)
Endif

If ( nInt % 2 == 0 )
   Return(.t.)
Endif

Return(.f.)


Code (fw): Select all Collapse
WORKVIEW DIALOG 12, 57, 169, 156
STYLE WS_POPUP | WS_CAPTION | WS_SYSMENU
FONT 6, "MS Sans Serif"
{
 CONTROL "Close", 140, "TBtnBmp", 32 | WS_CHILD | WS_VISIBLE | WS_TABSTOP, 102, 77, 41, 48
 CTEXT "Timer", 168, 5, 34, 27, 11, SS_CENTER | SS_NOPREFIX | WS_GROUP
 EDITTEXT 184, 5, 47, 49, 12, ES_AUTOHSCROLL | NOT WS_TABSTOP | WS_BORDER
 CONTROL "Timer", 180, "TBtnBmp", 32 | WS_CHILD | WS_VISIBLE | WS_TABSTOP, 59, 36, 35, 30
}
Posts: 2365
Joined: Wed Nov 02, 2005 11:46 PM
Re: Timer Hash Problem for Daniel
Posted: Mon Nov 05, 2012 10:13 PM

Rick

change to HEVAL( hObj, {| key, val | val:refresh() } ) )

remember activate the timer

ACTIVATE TIMER oTmr

Posts: 2706
Joined: Fri Oct 07, 2005 01:50 PM
Re: Timer Hash Problem for Daniel
Posted: Mon Nov 05, 2012 10:47 PM

Daniel and Antonio

Thank you both .. I did have the ACTIVATE TIMER oTmr rem'd out .. thinking too hard and over looked the obvious!

Rick Lipkin

Continue the discussion