Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dateTimePicker1 Equals DateTime.Now 2

Status
Not open for further replies.

jtk55

Programmer
Oct 11, 2008
4
AU
Hi all. I am trying to figure something out, I want to raise an event when the time and date in dateTimePicker1 is equal to DateTime.Now (Current date and time). Ex:

If user sets date and time using the dateTimePicker1 control, a messagebox will be displayed when the current date and time is equal to the date and time specified by the user in the dateTimePicker1 control.

Here's what I have right now (it ain't workin'):

if( dateTimePicker1.Value == DateTime.Now )
{
MessageBox.Show("Alert!");
}
else
{
Application.DoEvents();
}


Any help at all will be greatly appreciated! Thanks in advance!

regards,
jt
 
DateTime.Now returns the time as well.
Your datetime picker is returning only the date I assume.
They'ld have to pretty lucky to select the correct millisecond :p
 
:p No. What I need to do is kind of like an alarm clock. The user specifies a time that they want the alarm to go off... And then when the current time is equal to the time that the user has specified using the dateTimePicker an event is raised... I didn't completely get what you were trying to say in your last message hehe...
 
he is saying
Code:
if( dateTimePicker1.Value == DateTime.Now )
   {
     MessageBox.Show("Alert!");
   }
   else
   {
    Application.DoEvents();
   }
will always evaluate false. the chances of someone entering the correct second/millisecond are improbable.

you will need an event manager object at the application level to coordinate all events. all the form should do is create a new event and add it to the event manager object.
on some interval the event manager object will cycle through all the events determining if the event should be executed. if so the event should fire and then be removed from the list of events.

Code:
DateTime
someInstanceOfEventManager.AddEvent(new SomeSpecificEvent(dateTimePicker1.Value, "event fired");
Code:
public class EventManager
{
   private readonly Timer timer;
   private readonly List<IEvent> events = new List<IEvent>();

   public EventManager(Timer timer)
   {
      this.timer = timer;
      this.timer.Elasped += ExecuteEvents;
   }

   public void AddEvent(IEvent _event)
   {
      events.Add(_event);
   }

   private void ExecuteEvents(object sender, TimerElaspedEventArgs e)
   {
      List<IEvent> remove = new List<IEvent>();
      IEvent[] localEvents = events.ToArray();
      foreach(IEvent _event in localEvents)
      {
         if(!_event.ShouldExecute) continue;
         _event.Execute();
         events.Remove(_event);
      }
   }
}
Code:
interface IEvent
{
   bool ShouldExecute { get; }
   void Execute();
}

//example of a concrete event
class SomeEvent : IEvent
{
   public SomeEvent(DateTime executeAfter, string messageToLog)
   {
      this.executeAfter = executeAfter;
      this.messageToLog = messageToLog;
   }

   public bool ShouldExecute 
   { 
      get { return executeAfter >= DateTime.Now; } 
   }

   public void Execute()
   {
      Log.For(this).Debug(messageToLog);
   }
}

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
n3xus and jmeckly, thanks for takin' the time to help me! i appreciate your help and i got it to work :) yay!

jt.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top