Implement Timer Functionality

Hmanjarawala
Posted by Hmanjarawala under C# category on | Points: 40 | Views : 2549
This code snipet implements timer functionality little bit. I used .Net timer control which elapsed after some interval. on elapsed i update hour, minute and second value and print them in lable asynchronously through delegate.

for better understanding plz Copy this code and paste it in Visual Studio C#.Net project or free to ask if any query arise.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Timer = System.Timers.Timer;
using ElapsedEventArgs = System.Timers.ElapsedEventArgs;

namespace MyApplication
{
public partial class Form2 : Form
{
Timer tStart;
int mm = 0;
int ss = 0;
int hh = 0;
int ff = 0;

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form2());
}

public Form2()
{
InitializeComponent();
tStart = new Timer(200);
tStart.Elapsed += new System.Timers.ElapsedEventHandler(tStart_Elapsed);
}

private void tStart_Elapsed(object sender, ElapsedEventArgs e)
{
ff += 20;

if (ff >= 100)
{
ss += 1;
ff = 0;
}
if (ss > 59)
{
mm += 1;
ss = 0;
}
if (mm > 59)
{
hh += 1;
mm = 0;
}
UpdateStatus(lblTime, hh, mm, ss);
}

private delegate void delUpdateStatus(Label lbl, int hh, int mm, int ss);
private void UpdateStatus(Label lbl, int hh, int mm, int ss)
{
if (lbl.InvokeRequired)
{
lbl.Invoke(new delUpdateStatus(UpdateStatus), lbl, hh, mm, ss);
}
else
{
string text = string.Format("{0}:{1}:{2}", hh.ToString().PadLeft(2, '0'), mm.ToString().PadLeft(2, '0'), ss.ToString().PadLeft(2, '0'));
lbl.Text = text;
lbl.Update();
lbl.Parent.Update();
}
}

private void btnStart_Click(object sender, EventArgs e)
{
if (btnStart.Text == "Start")
{
btnStart.Text = "Stop";
tStart.Enabled = true;
tStart.Start();
}
else
{
btnStart.Text = "Start";
tStart.Stop();
tStart.Enabled = false;
}
}
}
}


namespace MyApplication
{
partial class Form2
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (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.lblTime = new System.Windows.Forms.Label();
this.btnStart = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// lblTime
//
this.lblTime.AutoSize = true;
this.lblTime.Font = new System.Drawing.Font("Verdana", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.lblTime.ForeColor = System.Drawing.Color.DarkBlue;
this.lblTime.Location = new System.Drawing.Point(12, 9);
this.lblTime.Name = "lblTime";
this.lblTime.Size = new System.Drawing.Size(15, 16);
this.lblTime.TabIndex = 1;
this.lblTime.Text = "-";
//
// btnStart
//
this.btnStart.Location = new System.Drawing.Point(18, 72);
this.btnStart.Name = "btnStart";
this.btnStart.Size = new System.Drawing.Size(75, 35);
this.btnStart.TabIndex = 2;
this.btnStart.Text = "Start";
this.btnStart.UseVisualStyleBackColor = true;
this.btnStart.Click += new System.EventHandler(this.btnStart_Click);
//
// Form2
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(115, 118);
this.Controls.Add(this.btnStart);
this.Controls.Add(this.lblTime);
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "Form2";
this.Text = "Form2";
this.ResumeLayout(false);
this.PerformLayout();

}

#endregion

private System.Windows.Forms.Label lblTime;
private System.Windows.Forms.Button btnStart;

}
}

Comments or Responses

Posted by: SheoNarayan on: 11/23/2011 Level:HonoraryPlatinum | Status: [Administrator] | Points: 10
Hi Hmanjarawala,

Thanks for submitting the code snippet, it would be nice for readers to understand if you can explain your code a bit.

Hope you would do that soon.

Thanks
Posted by: T.saravanan on: 11/26/2011 Level:Silver | Status: [Member] [MVP] | Points: 10
Hi Hmanjarawala,

Kindly submit a code inside the code tag.

Login to post response