Solved

C# Timer Question - Premature ticking???

Posted on 2004-09-20
5
530 Views
Last Modified: 2008-01-09
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
Comment
Question by:slimbx
  • 3
5 Comments
 

Author Comment

by:slimbx
ID: 12106385
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
 
LVL 3

Expert Comment

by:gillgates
ID: 12107308
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
 
LVL 3

Expert Comment

by:gillgates
ID: 12107387
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
 
LVL 1

Accepted Solution

by:
blueforce earned 25 total points
ID: 12108156
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
 
LVL 3

Expert Comment

by:gillgates
ID: 12108229
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This video discusses moving either the default database or any database to a new volume.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now