Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C# Timer Question - Premature ticking???

Posted on 2004-09-20
5
Medium Priority
?
572 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 100 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

704 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