Synchronizing Timer with windows time


In this article I explain about how to Synchronizing Timer with windows time

This code will sync the timer to windows time (seconds) and fires a event every Quater Hour

this code will be helpful if you want to fire a event that is related with windows time, like an alarm.

Used C# .net 2005



using System;
using System.Windows.Forms;

namespace sync_timer_windowstime
{

public partial class Form1 : Form
{
public int j; // flag
public Form1()
{
j = 0;

InitializeComponent();
}

private void timer1_Tick(object sender, EventArgs e)
{
if (DateTime.Now.Second == 00 && j == 0)
{
timer1.Stop();
timer1.Interval = 30000; // give what ever time interval u want the timer to execute
timer1.Start();
j = 1;// flag j to 1 as to prevent from Synchronizing again and again
}

if (j==1 && (DateTime.Now.Minute == 00 || DateTime.Now.Minute == 15 || DateTime.Now.Minute == 30 || DateTime.Now.Minute == 45))
{
// write the code u want to execute every Quater Hour
}
}

private void Form1_Load(object sender, EventArgs e)
{
timer1.Enabled = true; // enable the timer
timer1.Interval = 1000; //initilizing timer to fire every 1 sec
}

}
}


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: