Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C# (http://www.programmingforums.org/forum16.html)
-   -   scheduled events (http://www.programmingforums.org/showthread.php?t=15490)

hbe02 Mar 26th, 2008 6:25 PM

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!

Freaky Chris Mar 26th, 2008 7:09 PM

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

:

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


hbe02 Mar 28th, 2008 5:46 PM

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?

Freaky Chris Mar 28th, 2008 5:59 PM

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

Jabo Mar 28th, 2008 7:39 PM

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.

OpenLoop Mar 29th, 2008 8:30 AM

Re: scheduled events
 
Create a timer in your class:
:

  1. public partial class Form1 : Form
  2.     {...
  3.         Timer timer;

Initialize the timer in the class constructor:
:

  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:
:

  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. }


Ooble Mar 29th, 2008 10:19 PM

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.

Jimbo Mar 29th, 2008 11:58 PM

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.

Ooble Mar 30th, 2008 1:14 AM

Re: scheduled events
 
And if the clock's wrong and the user changes it while you're sleeping?

Jimbo Mar 30th, 2008 1:51 AM

Re: scheduled events
 
Quote:

Originally Posted by Ooble (Post 143216)
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 ;)


All times are GMT -5. The time now is 7:38 AM.

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