Here's a quick timer example with a Forms application
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace UsingTimer
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private Timer timer = new Timer();
private System.Windows.Forms.TextBox m_Interval;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button m_StartStop;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
timer.Tick += new EventHandler(timer_Tick);
}
/// <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.m_StartStop = new System.Windows.Forms.Button();
this.m_Interval = new System.Windows.Forms.TextBox();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// m_StartStop
//
this.m_StartStop.Location = new System.Drawing.Point(248, 16);
this.m_StartStop.Name = "m_StartStop";
this.m_StartStop.TabIndex = 0;
this.m_StartStop.Text = "StartTimer";
this.m_StartStop.Click += new System.EventHandler(this.button1_Click);
//
// m_Interval
//
this.m_Interval.Location = new System.Drawing.Point(16, 16);
this.m_Interval.Name = "m_Interval";
this.m_Interval.TabIndex = 1;
this.m_Interval.Text = "";
//
// button2
//
this.button2.Location = new System.Drawing.Point(160, 16);
this.button2.Name = "button2";
this.button2.TabIndex = 2;
this.button2.Text = "Set Interval";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(360, 70);
this.Controls.Add(this.button2);
this.Controls.Add(this.m_Interval);
this.Controls.Add(this.m_StartStop);
this.Name = "Form1";
this.Text = "Timer Example";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button2_Click(object sender, System.EventArgs e)
{
int interval = Int32.Parse(m_Interval.Text);
if ( interval < 1000 )
{
MessageBox.Show(
this,
"Interval are in milliseconds. You probably want to specify at least one "+
"thousand milliseconds otherwise you wont' be able to close this
application",
"ERROR",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
return;
}
timer.Interval = interval;
}
private void timer_Tick(object sender, EventArgs e)
{
timer.Stop();
MessageBox.Show("The timer has gone off");
timer.Start();
}
private void button1_Click(object sender, System.EventArgs e)
{
if ( this.m_StartStop.Text == "StartTimer" )
{
timer.Start();
m_StartStop.Text = "StopTimer";
}
else
{
timer.Stop();
m_StartStop.Text = "StartTimer";
}
}
}
}
--
Jared Parsons [MSFT]
(e-mail address removed)
This posting is provided "AS IS" with no warranties, and confers no rights.
OR if you wish to include a script sample in your post please add "Use of
included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm"
Afei said:
hi,
I want to write a program that every some specific time(say, 15
minutes) do something. But i am out of mind how to do it? Should i
use threads or some other technologies?
Thanks.