Re: DateTime.Value.CompareTo in c#

  • From: "Martin Slack" <m.g.slack@xxxxxxxxxxxx>
  • To: <programmingblind@xxxxxxxxxxxxx>
  • Date: Sat, 27 Oct 2007 21:24:32 +0100

Hi Andy,

I'm not sure how you are initialising your Time variable, so I've tried what
is probably a completely different approach.  See below for the contents of
the .cs file.

 I found the whole thing to be very sensitive and wasn't able to identify
all the troubles. I ended up synchronising the timer to the system time
which seems to make the CompareTo function much better behaved.

 I just have a form with a date time picker showing the time, and a timer.

---code starts---
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace Andy_B
 public partial class Form1 : Form
   [DllImport ("kernel32.dll")]
   public static extern bool Beep (uint dwFreq, uint dwDuration);
   DateTime dtTarget;

   public Form1 ()
     InitializeComponent ();

     // synchronise timer with system time
     timer1.Interval = 1;
     while (DateTime.Now.Millisecond > 0)
       // do nothing

     // set initial target time
     dtTarget = DateTime.Now.AddSeconds (10);

     // switch timer interval to seconds - 1000 causes trouble
     timer1.Interval = 990;

   private void dateTimePicker1_MouseDown (object sender, MouseEventArgs e)
     // select a new target time
     // change the time using the arrow keys
     // then click on the date time picker
     dtTarget = dateTimePicker1.Value;

   private void timer1_Tick (object sender, EventArgs e)
     // test system time against target time
     int i = DateTime.Now.CompareTo (dtTarget);

     if(i == 0)
       MessageBox.Show ("This is the second!");
       // write the system time into the window title bar
       Text = DateTime.Now.TimeOfDay.ToString ();

       // uncomment next line for beeps
       // Beep (440, 10);
---code ends---



----- Original Message ----- From: "Andy B" <a_borka@xxxxxxxxxxxxx>
To: <programmingblind@xxxxxxxxxxxxx>
Sent: Friday, October 26, 2007 6:03 PM
Subject: RE: DateTime.Value.CompareTo in c#


I set the DateTimePicker to 10/26/2007 3:20:00pm. My system time is set to
10/26/2007 12:58:25pm as of the test. It still shows the box every tick. The
goal is to get the box to show only once when the system time reaches the
DateTimePicker value. Don't know what is going wrong.....


View the list's information and change your settings at //

Other related posts: