Click here to Skip to main content
15,917,862 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am using c#. i has problem in this code:
C#
private void Form4_Load(object sender, EventArgs e)
       {
           timer1.Enabled = true;
       }

  private void timer1_Tick(object sender, EventArgs e)
       {
           label1.Text = DateTime.Now.Hour + ":" + DateTime.Now.Minute
                                     + ":" + DateTime.Now.Second;
           string time = DateTime.Now.Hour + ":" + DateTime.Now.Minute
                                          + ":" + DateTime.Now.Second;

           if (time == "17:19:50")
           {
               timer1.Enabled = false;
               Form4 frm3 = this;
               this.Close();
               MessageBox.Show("the time for the program finish");
               Form1 frm1 = new Form1();
               frm1.Show()}
               timer1.Enable =false ;

sometimes when i run that code the timer do not execute the if statement,why???
***note:i use c# with access database.
Posted
Updated 3-Jul-13 6:30am
v2
Comments
ZurdoDev 3-Jul-13 12:38pm    
Because it does not match the time?
loai_maane 3-Jul-13 12:40pm    
sometimes work no problem ,so what do you mean of not match???
ZurdoDev 3-Jul-13 12:45pm    
What do you mean? Just put a breakpoint and see what is happening.
Fredrik Bornander 3-Jul-13 12:38pm    
When you say "do not execute the if-statement", do you mean that expression is not evaluated to true even when you think it should be?

What is the interval of timer1?
loai_maane 3-Jul-13 12:39pm    
the interval 100
sometimes work no problem ,but sometimes do not work

First, it depends on the timer that you are using. If the timer has a resolution of 1000ms (1 second) then its not guaranteed to fire on the second.

For example, if the timer fires at second #5.5, it may fire again at second 6.6, which rounds up to second #7, so it can skip seconds. You can try increasing your timer resolution to get around this.

The second part to this answer is that windows forms timers are very poor for timing. The best way to do what you are wanting is to check for the passage through the time that you are wanting (set a flag that its before the time, then when that flag is true and its after your trigger time, do your operation).

[Edit] How to do this (even easier because no flag)

C#
public class Form4 : Form
{
    DateTime runTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 17, 19, 50, 0);

    public Form4()
    {
        InitializeComponent();
        timer1.Enabled = true;
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        if (DateTime.Now > runTime)
        {
            //Do what you need, then

            runTime = runTime.AddDays(1);
        }
    }
}
 
Share this answer
 
v3
Comments
loai_maane 3-Jul-13 12:44pm    
for your second part how can i do it ?? you have any examples please.
Ron Beyer 3-Jul-13 13:14pm    
I updated the solution with an example.
loai_maane 3-Jul-13 14:03pm    
thnx very much Ron
You're also not comparing the time correctly. You're comparing two strings, not two times.

For a schedule like this you don't compare for equality because it will only be equal for 1 second. If the comparison isn't done in that time, it'll fail. You compare the current time to see if it is greater than the scheduled time, not equal to it. Now the comparison will work at any time. Again, you compare DateTime objects, not strings to do this.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900