Solved

C# Timer Question - Premature ticking???

Posted on 2004-09-20
5
559 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

734 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