Solved

# Algorithm for reminder + mysql

Posted on 2004-10-12
515 Views
Hi everyone,

I'm trying to create a reminder for myself where the data is stored in mysql so that wherever I run the program, the data is always up to date.

My question is fairly simple: what is the algorithm for a reminder program? Do I need to set a timer in my program so that my program keeps checking the time (every minute)? Will it eat up my computer resources if I do that??

0
Question by:dFs1978
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 5
• 4
• 3
• +1

LVL 86

Accepted Solution

Mike Tomlinson earned 63 total points
ID: 12294968
That's exactly how you do it.  You simply poll the current time and compare it against your target times.  If the current time is greater than or equal to one of your target times then you have reached that time.

How efficient the algorithm is really depends on how you implement it.  If you have an actual timer control in your development language, then setting its Interval property to one second will be sufficient and will not eat up your computer resources.

If you don't have a timer control and must poll the current time in a While loop, then you must be a little more careful since an infinite looping structure like this has the potential to eat up your available CPU cycles.  On a windows machine, you could call the Sleep() API with a small interval of about 50 milliseconds to prevent your loop from ramping the CPU usage to 100%.

What language will you be using?

~IM
0

Expert Comment

ID: 12295245
Yeah, that right.  You could use a timer in order to checking the time (in every minute). If you worry that will burn out all of your resource, I suggest you one easy solution.
That you create in database a table with only one field that store date-time of the last time data updated.
Then from the app, when the timer-event occurs, you just need to check the date-time of DB with the last update date-time variable in your app. If they are different, this mean there are some changes on DB. Only then, you call update the whole data.
Won't take much of your resources. :-)
0

LVL 11

Assisted Solution

pratap_r earned 62 total points
ID: 12296988
umm.. there are better algos..

heres one.. when you know when you want to be reminded... (the time) which is available in your database.. you can put your process to sleep till that time.. and also make it wait for a signal (just incase a new reminder is added)

poling the database every second... not a good idea

tell me the language you are planing to do it in.. ill conjure up some code :-)

Pratap
0

Author Comment

ID: 12298269
Thanks everyone,

The language I'm using is Delphi. Like pratap_r said, I don't want my program to keep checking the database every minute.

pratap_r, I'm interested in your idea to make the program wait for a signal. My concern now is do I need some kind of server program to send the signal whenever a new data is added? I was thinking if I want several people to use the program using one database, the server needs to send the signal to all "active" program, doesnt it??

To be honest I prefer not to use the server-client method since I will have a problem to bypass the firewall in my office. The reason why I need to create this program is because I need to be reminded both at home and at work.

Thanks
0

LVL 11

Expert Comment

ID: 12299702
hmm now if you dont want to use the a client server model.. like a socket or something then polling the database is your only option.. since you have mentioned that you might work across networks.. and especially since you are behind a firewall

may be you should try http tunneling for the proxy? this will help you operate over port 80 which is usually allowed through firewalls..

--
Pratap
0

LVL 86

Expert Comment

ID: 12299894
>> poling the database every second... not a good idea

I quite agree and should qualify what I proposed a little clearer.  When the app loads, you query your database and build some structures in memory to represent the target times.  It is these structures, not the database that you query every second.  Polling structures in memory every second will not be CPU intensive but you could poll them every minute if you like.  You could create a class to represent your event times but I am not a Delphi programmer so I can't help you with that part.

You wouldn't want to load all the times in the database.  And obviously, you wouldn't want to load anything from before your current date/time.  You probably wouldn't want to load everything beyond your current date/time either since you may have things scheduled very far out into the future.

Perhaps you can query the database at application load and at specific intervals, say 15 minutes or so, for any events that will occur in the next couple hours.  Then you can compare the results with what you already have in memory and update if necessary.

~IM
0

Author Comment

ID: 12305434
I totally agree with you guys.

What I'm gonna do now is:
1. On load:  query tha database for events that haven't passed yet (max 2-3 events), then load into memory.
2. Set timer every minute to check events in memory
3. Set timer every 15 mins to check new events in database.
4. Prevent people to add new event that will happen for the next 20 mins (to allow other active programs to poll new event).
5. When an event happens, sound the alarm, then query database for events.

Does it sound good?? Basically I am going to have maximum 2-3 events in memory in case the server to database goes down, the program can still run for the next 2-3 events :)

0

LVL 11

Expert Comment

ID: 12305548
hey dFS instead of trying to chck the events in memory every  minute it would be better for you to sleep till that time.. for example the next reminder is to go off in 1 hr.. you can put your process to sleep for 60*60 seconds.. this will reduce the load on the cpu also..

now for poling the database.. dont refresh the entire event base.. try to use a select top 10 or something so that you allways have a good number of events..

something of the order of
select top 10 * from reminders where remindertime > current_time

again instead of fetching 10 records always you could optimise it in such a way that it picks the records only when there is a new record available.. a timestamp column could be used for this.. :-)

there are endless possiblities :-)

Good Luck!

enjoy!
Pratap
0

LVL 86

Expert Comment

ID: 12308262
Hit pratap_r,

The only reason I don't suggest putting the app to sleep until the next event is that it renders the app unresponsive.  Yes, it will be extremely efficient in that it won't use any CPU cycles, but what if you want for example to allow the app to view and cancel the next X events?  If the app is in a sleep state its UI won't respond at all.

~IM
0

LVL 11

Expert Comment

ID: 12309352
well if there is a UI then its a different case...probably a seperate thread!

Pratap
0

Author Comment

ID: 12309888
I think I'm going to use a timer and set the interval to the next event. That should work just fine, right Idle_Mind?? Or you still believe that setting the interval every minute is the best approach??
0

LVL 86

Expert Comment

ID: 12311646
That really depends on the capabilities or limitations of the Timer control.  If you can set its interval out far enough, then that would be perfect.  In VB6, the timer control has a limitation of just over one minute which means you would have to poll the current time for anything beyond that.  In VB.Net, there is a timer that can have a max interval of 2,147,483,647 milliseconds which is almost 25 days.

I don't know what kind of timer capability there is built into Delphi.  You would have to check the documentation.  If the timer control is flexible enough, then making it wait the entire interval would be just as good as putting the app to sleep for that period as pratap_r has suggested.

~IM
0

LVL 11

Expert Comment

ID: 12827298
the author accepted a combination of approaches suggested by me and idle_mind, I guess a split is in order

Have Fun!
Pratap
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

### Suggested Solutions

A short article about a problem I had getting the GPS LocationListener working.
A short article about problems I had with the new location API and permissions in Marshmallow
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and â€¦
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files â€” any plâ€¦
###### Suggested Courses
Course of the Month6 days, 8 hours left to enroll