title: C# Windows.Forms.Timer doesnt work properly
body: <p>I have some troubles while making simple application on C# / Windows.Forms that simulates a pendulum. </p>
  
  <p>It's a simple application with all backend logic in form code.
  First of all, I set up the interval equals 100 milliseconds (line 60), but when I calculate number of turns (line 155) and display it, displaying message:</p>
  
  <ol>
  <li>refresh every second (not every 100 milliseconds) </li>
  <li>and it behaves like this: 1-2-4-5-6-8 or 1-3-4-5-7-8 etc. </li>
  </ol>
  
  <p>I don't know when it started bugging and want to determine the mistake.</p>
  
  <p>Thanks a lot for your help.</p>
  
  <pre><code>using System;
  using System.Drawing;
  using System.Windows.Forms;
  
  namespace Koban_Lab1
  {
  public partial class mainWindow : Form
  {
      public Point pendFulcrum = new Point(150, 0);
      public Point pendTip = new Point();
      private bool deviceState_;
      private long ticks_;
      private DateTime startTiming;
      private Pen blackPencil;
      public static double g = 9.8;
      public double T;
      public double periodInTicks;
      public double L;
      public double a;
      public double Amp;
      public double startOffset;
      public double w;
      public double periodCounter;
      public double x;
      public double y;
      public double currentAngle;
  
      private Timer timer_;
      public double t;
  
      public Bitmap field;
  
      private long cnt = 0;
      private bool DeviceState
      {
          get
          {
              return deviceState_;
          }
          set
          {
              if (value == deviceState_)
              {
                  return;
              }
  
              deviceState_ = value;
              switch (value)
              {
                  case true:
                      L = Convert.ToDouble(pendulumLengthTextBox.Text) / 100;
                      a = Convert.ToDouble(plummetDistanceTextBox.Text) / 100;
                      periodCounter = 0;
                      T = 2 * Math.PI * Math.Sqrt((L * L) / (12 * g * a) + a / g);
                      w = 2 * Math.PI / T;
                      Amp = 2 * Math.PI * L / 24;
                      startOffset = Amp;
                      startTiming = DateTime.Now;
                      timer_ = new Timer();
                      timer_.Interval = 100;
                      timer_.Tick += new EventHandler(tickTimer);
                      timer_.Start();
                      startPendulumButton.Enabled = false;
                      stopPendulumButton.Enabled = true;
                      pendulumLengthTextBox.Enabled = false;
                      plummetDistanceTextBox.Enabled = false;
                      addReadoutButton.Enabled = true;
                      break;
                  case false:
                      timer_.Stop();
                      timer_ = null;
                      startPendulumButton.Enabled = true;
                      stopPendulumButton.Enabled = false;
                      pendulumLengthTextBox.Enabled = true;
                      plummetDistanceTextBox.Enabled = true;
                      timerTextBox1.Text = Locale.timerDisabledMessage;
                      timerTextBox2.Text = "0";
                      addReadoutButton.Enabled = false;
                      break;
              }
  
              return;
          }
      }
  
      public Graphics fieldGraphics;
  
      public mainWindow()
      {
          InitializeComponent();
      }
  
      private void Form1_Load(object sender, EventArgs e)
      {
          pendulumLengthTooltip.SetToolTip(pendulumLengthTextBox, Locale.pendulumLengthTooltipMessage);
          plummetDistanceTooltip.SetToolTip(plummetDistanceTextBox, Locale.plummetDistanceTooltipMessage);
          blackPencil = Pens.Gray;
          timerTextBox1.Text = Locale.timerDisabledMessage;
          timerTextBox2.Text = "0";
          deleteReadoutButton.Enabled = false;
          DeviceState = false;
          field = new Bitmap(pendulumField.Width, pendulumField.Height);
      }
  
      private void startPendulumButton_Click(object sender, EventArgs e)
      {
          DeviceState = true;
      }
  
      private void stopPendulumButton_Click(object sender, EventArgs e)
      {
          DeviceState = false;
      }
  
      private void addReadoutButton_Click(object sender, EventArgs e)
      {
          if (timerTextBox1.Text == Locale.timerDisabledMessage)
          {
              return;
          }
  
          readoutList.Items.Add(timerTextBox1.Text);
          deleteReadoutButton.Enabled = true;
  
          return;
      }
  
      private void deleteReadoutButton_Click(object sender, EventArgs e)
      {
          if (readoutList.SelectedIndex != (-1))
          {
              readoutList.Items.RemoveAt(readoutList.SelectedIndex);
          }
          else
          {
              readoutList.Items.RemoveAt(readoutList.Items.Count - 1);
          }
  
          if (readoutList.Items.Count == 0)
          {
              deleteReadoutButton.Enabled = false;
          }
      }
  
  
  
      private void tickTimer(object sender, EventArgs e)
      {
          ticks_ = DateTime.Now.Ticks - startTiming.Ticks;
          timerTextBox1.Text = TimeSpan.FromTicks(ticks_).ToString(@"mm\:ss\:ff");
          timerTextBox2.Text = periodCounter.ToString();
  
          ticks_ = DateTime.Now.Ticks - startTiming.Ticks;
          t = ticks_ / TimeSpan.TicksPerSecond;
          periodCounter = t / T;
          timerTextBox2.Text = Convert.ToInt32(periodCounter).ToString();
  
  
          currentAngle = Amp * Math.Sin(w * t);
          x = x * Math.Cos(currentAngle) - y * Math.Sin(currentAngle);
          y = x * Math.Sin(currentAngle) + y * Math.Cos(currentAngle);
  
          Program.labWindow.fieldGraphics = Graphics.FromHwnd(Program.labWindow.pendulumField.Handle);
          pendTip.X = (int)x;
          pendTip.Y = (int)y;
          fieldGraphics.DrawLine(blackPencil, pendFulcrum, pendTip);
  
          pendulumField.Image = field;
          Application.DoEvents();
      }
  
      private void plummetDistanceTooltip_Popup(object sender, PopupEventArgs e)
      {
  
      }
  
      private void plummetDistanceLabel_Click(object sender, EventArgs e)
      {
  
      }
  }
  }
  </code></pre>