C# Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
namespace ThreadingUsingTimer
{
public class ThreadTimer
{
public static void Main(string[] args)
{
Timer time = new Timer(); // Doesn't require any argument
time.Interval = 1000;
time.Elapsed += time_Elapsed; //Use an event
time.Start(); // Start the timer
Console.ReadLine();
time.Stop();
Console.WriteLine("Timer Thread stopped.Enter again to start.\n Twice entering would destroy the Thread.");
// Pause the Timer
Console.ReadLine();
time.Start(); // Resume the timer
Console.ReadLine();
Console.WriteLine("Thread destroyed!");
time.Dispose();
Console.ReadKey();
// Destroy the Timer Thread
}
public static void Display(object ObjTime)
{
//This run on a pooled thread
Console.WriteLine(ObjTime); // Wrtite "tick"
}
public static void time_Elapsed (object sender,EventArgs e)
{
Console.WriteLine("The Time is passing by :");
Console.WriteLine(DateTime.Now);
}
}
}
Output:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
namespace ThreadingUsingTimer
{
public class ThreadTimer
{
public static void Main(string[] args)
{
Timer time = new Timer(); // Doesn't require any argument
time.Interval = 1000;
time.Elapsed += time_Elapsed; //Use an event
time.Start(); // Start the timer
Console.ReadLine();
time.Stop();
Console.WriteLine("Timer Thread stopped.Enter again to start.\n Twice entering would destroy the Thread.");
// Pause the Timer
Console.ReadLine();
time.Start(); // Resume the timer
Console.ReadLine();
Console.WriteLine("Thread destroyed!");
time.Dispose();
Console.ReadKey();
// Destroy the Timer Thread
}
public static void Display(object ObjTime)
{
//This run on a pooled thread
Console.WriteLine(ObjTime); // Wrtite "tick"
}
public static void time_Elapsed (object sender,EventArgs e)
{
Console.WriteLine("The Time is passing by :");
Console.WriteLine(DateTime.Now);
}
}
}
Output:
No comments:
Post a Comment