Timer Class in C# example
In the following program, a thread with a timer is created. The Start and Stop methods of the timer are used to start and stop the timer and the thread.
Example:
x
34
34
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
using System.Timers;
5
namespace ThreadTimers
6
{
7
class Test
8
{
9
static void Main()
10
{
11
Timer t1=new Timer();
12
t1.Interval=1000;
13
t1.Elapsed+=t1_Elapsed;
14
t1.Start();
15
Console.ReadLine();
16
t1.Stop();
17
Console.WriteLine("Timer Thread Stopped !");
18
Console.ReadLine();
19
t1.Start();
20
Console.ReadLine();
21
Console.WriteLine("Thread Destroyed !");
22
t1.Dispose();
23
}
24
static void Display(object objTime)
25
{
26
Console.WriteLine(ObjTime);
27
}
28
static void t1_Elapsed(object sender, EventArgs e)
29
{
30
Console.Write("The Time is passing by: ");
31
Console.WriteLine(DateTime.Now);
32
}
33
}
34
}