none
Start timer and Check timeout in WinCE RRS feed

  • Question

  • Hi all,

    I'm porting a Linux driver to WEC7.

    I want to implement following in WinCE.

    start_timer();
    while(check_timer())
    {	
          //do some work
    }

    i.e.: If timer time-out, come out of loop.

    Which functions I can use to implement this?

    While searching in MSDN, I saw SetTimer. But MSDN page showing message that

    "This function is deprecated."

    So which functions I can use here?

    Thanks.

    With regards,

    Keshava G N


    Keshava G N Member - Technical (Software), iWave Systems, Bangalore <http://iwavesystems.com> mailto: keshavagn AT iwavesystems DOT com

    Wednesday, May 29, 2013 4:26 AM

Answers

  • Look at this timer implementation code in WEC7,

    ~\public\COMMON\oak\drivers\mmtimer

    Regards,

    Rakesh.

    • Marked as answer by Keshava GN Thursday, May 30, 2013 4:06 AM
    Wednesday, May 29, 2013 5:18 AM
  • Keshava, your GetTickCount() solution is excellent.  But using WaitForSingleObject() can work for you:

    start the timer.

    while( 1 )
    {
        DWORD result = WaitForSingleObject( event, 0 );
        if( result == WAIT_OBJECT_0 )
            break; \\ timer fired so quit - otherwise it was a timeout so keep running
    }


    Bruce Eitman (eMVP)
    Senior Engineer
    Bruce.Eitman AT Eurotech DOT com
    My BLOG http://geekswithblogs.net/bruceeitman

    Eurotech Inc.
    www.Eurotech.com


    Wednesday, May 29, 2013 12:38 PM
    Moderator
  • Hi Rakesh,

    Thanks for quick response.

    I looked into mmtimer code..

    But how i can use TimerEvents?

    Because, Waitforsingleobject will wait for specific time, but i need to do some action within the time..

    But while seeing code I got this:

    time_1 = GetTickCount();
    
    while (TIMEOUT_MS>(GetTickCount() - time_1))
    {
    	//do something
    }

    It it correct to do like this?

    With regards,

    Keshava G N


    Keshava G N Member - Technical (Software), iWave Systems, Bangalore <http://iwavesystems.com> mailto: keshavagn AT iwavesystems DOT com



    • Edited by Keshava GN Wednesday, May 29, 2013 6:17 AM
    • Marked as answer by Keshava GN Thursday, May 30, 2013 4:07 AM
    Wednesday, May 29, 2013 6:13 AM

All replies

  • Look at this timer implementation code in WEC7,

    ~\public\COMMON\oak\drivers\mmtimer

    Regards,

    Rakesh.

    • Marked as answer by Keshava GN Thursday, May 30, 2013 4:06 AM
    Wednesday, May 29, 2013 5:18 AM
  • Hi Rakesh,

    Thanks for quick response.

    I looked into mmtimer code..

    But how i can use TimerEvents?

    Because, Waitforsingleobject will wait for specific time, but i need to do some action within the time..

    But while seeing code I got this:

    time_1 = GetTickCount();
    
    while (TIMEOUT_MS>(GetTickCount() - time_1))
    {
    	//do something
    }

    It it correct to do like this?

    With regards,

    Keshava G N


    Keshava G N Member - Technical (Software), iWave Systems, Bangalore <http://iwavesystems.com> mailto: keshavagn AT iwavesystems DOT com



    • Edited by Keshava GN Wednesday, May 29, 2013 6:17 AM
    • Marked as answer by Keshava GN Thursday, May 30, 2013 4:07 AM
    Wednesday, May 29, 2013 6:13 AM
  • Keshava, your GetTickCount() solution is excellent.  But using WaitForSingleObject() can work for you:

    start the timer.

    while( 1 )
    {
        DWORD result = WaitForSingleObject( event, 0 );
        if( result == WAIT_OBJECT_0 )
            break; \\ timer fired so quit - otherwise it was a timeout so keep running
    }


    Bruce Eitman (eMVP)
    Senior Engineer
    Bruce.Eitman AT Eurotech DOT com
    My BLOG http://geekswithblogs.net/bruceeitman

    Eurotech Inc.
    www.Eurotech.com


    Wednesday, May 29, 2013 12:38 PM
    Moderator
  • Thank you Bruce...

    Here i have to setevent after TIMEOUT milliseconds, correct?

    Also, is it ok to use GetTickCount() ?

    Will it be slow? (As it reads TickCount continuously).


    Thanks..


    Keshava G N Member - Technical (Software), iWave Systems, Bangalore <http://iwavesystems.com> mailto: keshavagn AT iwavesystems DOT com


    • Edited by Keshava GN Wednesday, May 29, 2013 1:00 PM
    Wednesday, May 29, 2013 12:58 PM
  • It won't be "slow" necessarily but it will use processor resources when your code is running. Since you're doing something during the time-out period, I wouldn't worry about it. The processor effort to read the tick count is pretty small. One concern with the code as prototyped above is accounting for tick count roll-over which occurs every month or so. You need to plan for that.

    The main advantage of WaitForSingleObject() is accessibility from other threads to forcing your loop to stop. You can call SetEvent() from the main thread of an application or an IoControl handler of your driver and your loop will treat it as a time-out. You could even do it from another process if the event is named and both sides know the name.

    Paul T.

    Wednesday, May 29, 2013 3:22 PM