Solved

C# Timer Question - Premature ticking???

Posted on 2004-09-20
5
537 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
fomat Json objects 6 28
Word Directory is not in the drop down list 4 25
Turn on intranet settings 1 37
Wav problem 4 13
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

912 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

24 Experts available now in Live!

Get 1:1 Help Now