Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 26th, 2008, 6:25 PM   #1
hbe02
Hobbyist Programmer
 
hbe02's Avatar
 
Join Date: Mar 2006
Location: Lebanon
Posts: 148
Rep Power: 3 hbe02 is on a distinguished road
scheduled events

hi all,
it is to my knowledge that the timer component or class is used like some kind of count down timer that triggers an event. Now how can i instead trigger an event(function) at a certain time or date. like calender event. on 22/12/2009 at 11:00 am.
can this be done in an even triggered environment.. cause i can think of a very inefficient way of polling at every 5 minutes or so... but that is very wastefull!
hbe02 is offline   Reply With Quote
Old Mar 26th, 2008, 7:09 PM   #2
Freaky Chris
Hobbyist Programmer
 
Freaky Chris's Avatar
 
Join Date: Dec 2007
Location: England
Posts: 169
Rep Power: 1 Freaky Chris is on a distinguished road
Send a message via MSN to Freaky Chris
Re: scheduled events

just retrive the system clock time and the check to see if it equal to the time you are after... surely that would work no.

Somehting like this

C# Syntax (Toggle Plain Text)
  1. DateTime theDate = DateTime.Parse("27 Mar 2008 00:26:00");
  2. if (DateTime.Now => theDate)
  3. {
  4. MessageBox.Show((DateTime.Now).ToString());
  5. }

Last edited by Freaky Chris; Mar 26th, 2008 at 7:29 PM.
Freaky Chris is offline   Reply With Quote
Old Mar 28th, 2008, 5:46 PM   #3
hbe02
Hobbyist Programmer
 
hbe02's Avatar
 
Join Date: Mar 2006
Location: Lebanon
Posts: 148
Rep Power: 3 hbe02 is on a distinguished road
Re: scheduled events

yes that would definitely work. but you would have to poll that if statement on every second. isnt that kind of unpractical? isnt there some kind of event based approach?
hbe02 is offline   Reply With Quote
Old Mar 28th, 2008, 5:59 PM   #4
Freaky Chris
Hobbyist Programmer
 
Freaky Chris's Avatar
 
Join Date: Dec 2007
Location: England
Posts: 169
Rep Power: 1 Freaky Chris is on a distinguished road
Send a message via MSN to Freaky Chris
Re: scheduled events

if you were to use a timer you could acheive this perhaps,

so if you were to start a timer when the app loads, it checks the date that it should preform some function at. Compares it to the current date to check to see if it should run the function.
if not it callsback to itself and check again etc.

C# isn't really the area i code in so supply sample code is an issue but something like that should work...if not i'll re-think again
__________________
Who said i couldn't program
sarcasm = raw_input('Type in a sarcastic remark: ')
print sarcasm
Freaky Chris is offline   Reply With Quote
Old Mar 28th, 2008, 7:39 PM   #5
Jabo
Not a user?
 
Join Date: Sep 2007
Posts: 245
Rep Power: 1 Jabo is on a distinguished road
Re: scheduled events

In VB, and C# I'm sure, you can adjust the DateTime to just show the date and evaluate the date. So you don't have to poll every second, you could poll twice a day, and if it finds the date it's looking for, BOOM goes the dynamite.
Jabo is offline   Reply With Quote
Old Mar 29th, 2008, 8:30 AM   #6
OpenLoop
Expert Programmer
 
OpenLoop's Avatar
 
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4 OpenLoop is on a distinguished road
Re: scheduled events

Create a timer in your class:
c# Syntax (Toggle Plain Text)
  1. public partial class Form1 : Form
  2. {...
  3. Timer timer;
Initialize the timer in the class constructor:
c# Syntax (Toggle Plain Text)
  1. public Form1()
  2. {
  3. InitializeComponent();
  4. ...
  5. timer = new Timer();
  6. timer.Interval = 100; //calls the fuction specified below every 100ms (1/10 sec)
  7. timer.Tick += new EventHandler(CheckTime); //specify function to call every tick
  8. timer.Start(); //start the timer
  9. ...

create the function to be called every tick and put Chris's code in it:
c# Syntax (Toggle Plain Text)
  1. void CheckTime(object sender, EventArgs e)
  2. {
  3. DateTime theDate = DateTime.Parse("27 Mar 2008 00:26:00");
  4. if (DateTime.Now => theDate)
  5. {
  6. MessageBox.Show((DateTime.Now).ToString());
  7. timer.Stop(); //if the event is no longer to be checked for, stop the timer
  8. }
  9. }
OpenLoop is offline   Reply With Quote
Old Mar 29th, 2008, 10:19 PM   #7
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Re: scheduled events

There isn't a way to have a function called at a specific time - the only way to do it is to poll the clock. Of course, if you were to poll it once every minute or less, the processor time dedicated to your task would be so negligible that no one would care.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Mar 29th, 2008, 11:58 PM   #8
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 748
Rep Power: 3 Jimbo is on a distinguished road
Re: scheduled events

If you know the time you want it to fire at, you can sleep the thread for how long it takes to reach that time. Something along the lines of this pseudo-code:
DateTime target = "sometime";
Thread.Sleep(target - DateTime.Now);
// do your task

At work, we have a tool which basically runs on a timer every 15 seconds, checks an XML file to see if any of the tasks listed are due to run, and checks if any tasks have been added, then sleeps again. So, the polling is also a feasible design, give or take a few seconds.
__________________
<insert disclaimer here>
<insert shameless plug for Visual Studio here>
Jimbo is offline   Reply With Quote
Old Mar 30th, 2008, 1:14 AM   #9
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Re: scheduled events

And if the clock's wrong and the user changes it while you're sleeping?
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Mar 30th, 2008, 1:51 AM   #10
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 748
Rep Power: 3 Jimbo is on a distinguished road
Re: scheduled events

Quote:
Originally Posted by Ooble View Post
And if the clock's wrong and the user changes it while you're sleeping?
That will be a problem with any polling solution, unless you assume that the first method I mentioned is put into action when the clock is set correctly
__________________
<insert disclaimer here>
<insert shameless plug for Visual Studio here>
Jimbo is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Priority Queues Events kruptof C# 12 Sep 6th, 2007 5:56 PM
Implementation of Mouse Events. smita Existing Project Development 3 Mar 15th, 2007 3:11 PM
propagating mousemove events Jimbo JavaScript and Client-Side Browser Scripting 4 Sep 15th, 2006 1:20 PM
Clone Node Events Eryk JavaScript and Client-Side Browser Scripting 2 Feb 24th, 2006 2:15 PM
question: usage of delagates and custom events melbolt C# 1 Oct 3rd, 2005 7:17 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:24 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC