Solved

Algorithm for reminder + mysql

Posted on 2004-10-12
16
459 Views
Last Modified: 2012-08-13
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??

Thanks in advance

0
Comment
Question by:dFs1978
  • 5
  • 4
  • 3
  • +1
16 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 63 total points
Comment Utility
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

by:huyntminh
Comment Utility
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

by:pratap_r
pratap_r earned 62 total points
Comment Utility
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

by:dFs1978
Comment Utility
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

by:pratap_r
Comment Utility
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 85

Expert Comment

by:Mike Tomlinson
Comment Utility
>> 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:dFs1978
Comment Utility
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

by:pratap_r
Comment Utility
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 85

Expert Comment

by:Mike Tomlinson
Comment Utility
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

by:pratap_r
Comment Utility
well if there is a UI then its a different case...probably a seperate thread!

Pratap
0
 

Author Comment

by:dFs1978
Comment Utility
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 85

Expert Comment

by:Mike Tomlinson
Comment Utility
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

by:pratap_r
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now