Timer that fired on certain date/time? Topic is solved

Are you writing your own components and need help with how to set them up or have questions about the components you are deriving from ? Ask them here.
Post Reply
nargus
Earned a small fee
Earned a small fee
Posts: 18
Joined: Mon Jul 31, 2006 2:49 pm

Timer that fired on certain date/time?

Post by nargus »

Is there such component available? :O
If not, is there any suggestion on how should I create it? :O I am thinking about using wxTimer that fired every 86400 seconds (one full day), so it can check to that day's alert and set another timer to fire it...but that don't sound like the best choice...some how :roll:

I'm thinking about just set the timer to the certain date/time also. But then, I fear what would happen if the date/time happen to be someday in next 10 years later, not next week (Assuming the computer never shutdown, that is) :roll:

So...I'm wondering if anyone have any suggestion on the component I should use? o.o


Thank :)
Windows XP, MS Visual C++ 6, DialogBlocks 3.0, wxWidgets 2.7.0
Peer Sommerlund
Knows some wx things
Knows some wx things
Posts: 43
Joined: Tue Jun 13, 2006 7:21 am
Location: Denmark
Contact:

Post by Peer Sommerlund »

A simple solution would be to use wxTimerEvent, to check the timer every 60000 ms (= 1 minute). Your code could have multiple timers, you could alter the time of the alert while waiting for it to occur.

If you create a thread for each timer you can have the thread sleep until the alert is set of, then make the thread do something non-gui, or make it send an event to the gui-thread if you want to update the gui.

Using wxTimerEvent will probably be simpler :wink: : small code footprint, no thread problems, easy to manipulate gui, small cpu usage.
nargus
Earned a small fee
Earned a small fee
Posts: 18
Joined: Mon Jul 31, 2006 2:49 pm

Post by nargus »

Ah...maybe. I've just implement a small timer class though. I call wxScheduleTimer. And take wxDateTime as Start() parameter, instead of interval in millisecond. Hope it'll make my life easier :)

But recurring everying certain time to check sound interesting also. But a minute feel too short for me. Maybe I can do timer every an hour or so, then set up second one when there's a scheduled time within an hour?

wxScheduleTimer:

Code: Select all

/////////////////////////////////////////////////////////////////////////////
// Name:        scheduletimer.h
// Purpose:     wxScheduleTimer
// Author:      Annop "Nargus" Prapasapong
// Modified by: 
// Created:     8 August 2006
// RCS-ID:      
// Copyright:   (c) Annop "Nargus" Prapasapong
// Licence:     wxWindows licence
/////////////////////////////////////////////////////////////////////////////

#ifndef _WX_SCHEDULETIMER_H_
#define _WX_SCHEDULETIMER_H_

#include "wx/defs.h"

#if wxUSE_GUI && wxUSE_TIMER

#include "wx/timer.h"
#include "wx/datetime.h"

class WXDLLEXPORT wxScheduleTimer : public wxTimer {
public:

	// Start the timer that notify at given 'local' date/time
	// If the the time has passed; This function call wxTimer::Start(-1, true);
	virtual bool Start(const wxDateTime &datetime);

	// Return current timer's date/time
	inline const wxDateTime &GetDateTime() const;

	// Return number of milliseconds from now to scheduled timer
	// Negative value if the scheduled time has passed
	int GetScheduleInterval();

protected:
	wxDateTime m_datetime;
};

#endif // wxUSE_GUI && wxUSE_TIMER

#endif // _WX_SCHEDULETIMER_H_

Code: Select all

/////////////////////////////////////////////////////////////////////////////
// Name:        scheduletimer.cpp
// Purpose:     wxScheduleTimer
// Author:      Annop "Nargus" Prapasapong
// Modified by: 
// Created:     8 August 2006
// RCS-ID:      
// Copyright:   (c) Annop "Nargus" Prapasapong
// Licence:     wxWindows licence
/////////////////////////////////////////////////////////////////////////////

// ============================================================================
// declarations
// ============================================================================

// For compilers that support precompilation, includes "wx.h".
#include "wx/wxprec.h"

#ifdef __BORLANDC__
    #pragma hdrstop
#endif

#if wxUSE_TIMER

#ifndef WX_PRECOMP
    #include "scheduletimer.h"
#endif

// ============================================================================
// wxScheduleTimer implementation
// ============================================================================

bool wxScheduleTimer::Start(const wxDateTime &datetime){
    m_datetime = datetime;
	time_t interval = GetScheduleInterval();
	if(interval > 0){
		m_datetime = datetime;
		return wxTimer::Start(interval, true);
	}else return wxTimer::Start(-1, true);
}

inline const wxDateTime &wxScheduleTimer::GetDateTime() const {
	return m_datetime;
}

int wxScheduleTimer::GetScheduleInterval(){
    // Find number of seconds difference, and multiply by 1000 milliseconds
	return (m_datetime.GetTicks() - wxDateTime::GetTimeNow()) * 1000;
}

#endif // wxUSE_TIMER
Windows XP, MS Visual C++ 6, DialogBlocks 3.0, wxWidgets 2.7.0
Post Reply