Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / July 2005

Tip: Looking for answers? Try searching our database.

help with "rolling my own" Timer class

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Alfonso Morra - 19 Jul 2005 00:20 GMT
Hi,

I am writing a timer class that I want to be able to get to notify me
(via a callback func), when a specified interval has elapsed. I have
most of the timer functionality figured - however, I need to spawn a new
thread to carry out the "time watch" - and I need to do this in a cross
platform (Well Linux/Windows) way ...

Any help will be much appreciated. The code (snippet) follows below:

#include <ctime>

typedef void (*TIMER_CB_FUNC)( void ) ;

class Timer {
public:
    inline Timer():m_cbfunc(0),m_interval(0),m_stime(0){} ;
    Timer( TIMER_CB_FUNC, unsigned short) ;
    Timer( const Timer&) ;
    Timer& operator= (const Timer& ) ;
    virtual ~Timer() ; //not really required

private:
    TIMER_CB_FUNC    m_cbfunc ;
    unsigned short    m_interval ;
    time_t        m_stime ;

    /* private functions */
    void reset( void );
};

Basically when the Timer class is constructed, it must start a new
thread that waits till the time is up and then notifies me. MTIA
William DePalo [MVP VC++] - 19 Jul 2005 01:56 GMT
> I am writing a timer class that I want to be able to get to notify me (via
> a callback func), when a specified interval has elapsed. I have most of
> the timer functionality figured - however, I need to spawn a new thread to
> carry out the "time watch" - and I need to do this in a cross platform
> (Well Linux/Windows) way ...

I can't help you with the cross platform issues except to mention that there
are libraries that try to smooth over the platform differences like this one

http://www.cs.wustl.edu/~schmidt/ACE.html

and this one

http://www.roguewave.com/products/enterprise/

and this one

http://www.boost.org/doc/html/threads.html

On the Win32 side, it should be a no-brainer to wrap a class around the
multimedia function timeSetEvent() which dispatches callbacks at a future
time and which manages the lifetime of the thread on which the callbacks
occur.

Regards,
Will
Alfonso Morra - 19 Jul 2005 09:22 GMT
>>I am writing a timer class that I want to be able to get to notify me (via
>>a callback func), when a specified interval has elapsed. I have most of
[quoted text clipped - 22 lines]
> Regards,
> Will

Thanks Will for the post. I think to keep things simple for now, I'll
just use conditional compilation and branch the logic accross the
platforms. Would you be so kind as to get me started with the
timeSetEvent() wrapper, I'm currently reading up on the function notes
and I'm sure I'll be able to follow up on an example that you provide.

MTIA

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.