Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 576
  • Last Modified:

C# Timer Question - Premature ticking???

Hello all, having a C# timer issue, just one of those days today. How high can you set the interval in C# for a timer? I am trying to specify my interval in minutes and hours according to the code below:

iHourIntervalValue = (iHours * 60 * 1000);
iMinuteIntervalValue = (iMinutes * 60 * 60 * 1000);

But I have notice that when I specifed the interval in minutes, lets say to fire off every 15 minutes, that my timer fires every 3.5 minutes or so, not every 15 minutes. I have not even tested the hour interval, for fear that what I am doing with the minute timer is incorrect. When I start my code, I am creating an event handler for my timers tick property, that is when this happens. To start, I am doing the following:

tmrData.Interval = iMinuteIntervalValue;
tmrData.Tick+= new System.EventHandler(this.tmrData_Tick);
tmrData.Start(); tmrData.Enabled = true;

In tmrData_Tick, I am firing off a subroutine that calls my database to retrieve and return xml data back. This routine is firing every 3.5 minutes instead of 15 minutes, please help.

Thanks.  -slimbx




0
slimbx
Asked:
slimbx
  • 3
1 Solution
 
slimbxAuthor Commented:
P.S. - Forgot to add that in the tmrData_Tick method, I am doing tmrData.Enabled = false, then I run my sub, then I do tmrData.Enabled = true;


slimbx
0
 
gillgatesCommented:
Are you using the System.Thread.Timer class... I am not sure why your program is doing that buy you can try the System.Threading.Timer class....

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemthreadingtimerclasstopic.asp
0
 
gillgatesCommented:
I just tried it on my pc with the form control timer....  and it worked fine...

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication1
{
      /// <summary>
      /// Summary description for Form1.
      /// </summary>
      public class Form1 : System.Windows.Forms.Form
      {
            private System.Windows.Forms.Button button1;
            private System.Windows.Forms.Timer timer1;
            private System.ComponentModel.IContainer components;

            public Form1()
            {
                  //
                  // Required for Windows Form Designer support
                  //
                  InitializeComponent();

                  //
                  // TODO: Add any constructor code after InitializeComponent call
                  //
            }

            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            protected override void Dispose( bool disposing )
            {
                  if( disposing )
                  {
                        if (components != null)
                        {
                              components.Dispose();
                        }
                  }
                  base.Dispose( disposing );
            }

            #region Windows Form Designer generated code
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                  this.components = new System.ComponentModel.Container();
                  this.button1 = new System.Windows.Forms.Button();
                  this.timer1 = new System.Windows.Forms.Timer(this.components);
                  this.SuspendLayout();
                  //
                  // button1
                  //
                  this.button1.Location = new System.Drawing.Point(104, 112);
                  this.button1.Name = "button1";
                  this.button1.Size = new System.Drawing.Size(86, 10);
                  this.button1.TabIndex = 0;
                  this.button1.Text = "button1";
                  this.button1.Click += new System.EventHandler(this.button1_Click);
                  //
                  // timer1
                  //
                  this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
                  //
                  // Form1
                  //
                  this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
                  this.ClientSize = new System.Drawing.Size(292, 266);
                  this.Controls.Add(this.button1);
                  this.Name = "Form1";
                  this.Text = "Form1";
                  this.ResumeLayout(false);

            }
            #endregion

            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main()
            {
                  Application.Run(new Form1());
            }

            private void button1_Click(object sender, System.EventArgs e)
            {
                  int temp;

                  temp = 60 * 60 * 1000 * 15;
                  timer1.Interval = temp;
                  timer1.Enabled = true;
                  timer1.Start();
            }

            private void timer1_Tick(object sender, System.EventArgs e)
            {
                  MessageBox.Show("NOW");
            }
      }
}
0
 
blueforceCommented:
iHourIntervalValue = (iHours * 60 * 1000);
iMinuteIntervalValue = (iMinutes * 60 * 60 * 1000);


If I understand your code right, I understand something similar to this:

int iHours    = 3;
int iMinutes = 15;
int iHourIntervalValue = 0, iMinuteIntervalValue = 0;

iHourIntervalValue    = (iHours * 60 * 1000);             //  180000 milliseconds ( 3 minutes )
iMinuteIntervalValue = (iMinutes * 60 * 60 * 1000);   //  54000000 milliseconds ( 15 hours )

If that's the case - you've just got the math backwards.

I think it should look more like this:

int iHours    = 3;
int iMinutes = 15;
int iHourIntervalValue = 0, iMinuteIntervalValue = 0;

iMinuteIntervalValue    = (iMinutes * 60 * 1000);  // = 900000 milliseconds (15 minutes)
iHourIntervalValue = (iHours * 60 * 60 * 1000);   // = 10800000 milliseconds ( 3 hours )


Lastly, timer.Start() is exactly the same as timer.Enabled = true;  Using both on the same line, is redundant and requires 2 calls to the timer class's Enabled property.  You should actually avoid using Start() and Stop() altogether and only use Enabled = true/false.
0
 
gillgatesCommented:
actually in your first post you had it backwards (so did I in my example)... and I only used both .Start() & .Enabled = true because you did in your first example...

I noticed the hours, minutes thing and don't know why I did the same...

Did you try the System.Threading.Timer if you are still having the problem?
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now