Skip to content

Instantly share code, notes, and snippets.

@rtipton
Created March 20, 2010 03:02
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rtipton/338441 to your computer and use it in GitHub Desktop.
Save rtipton/338441 to your computer and use it in GitHub Desktop.
C# -- Timers in .NET
using System;
using System.Threading;
class Program
{
static void Main()
{
AutoResetEvent reset = new AutoResetEvent(false);
StatusChecker status = new StatusChecker(5);
// Invoke methods for the timer via a Delegate
TimerCallback timerDelegate = new TimerCallback(status.CheckStatus);
// Create a timer that signals the delegate to invoke
// Check status after one second, and then every 1/4 second
Console.WriteLine("{0} Creating the timer.\n",DateTime.Now.ToString("h:mm:ss.fff"));
Timer stateTimer = new Timer(timerDelegate, reset, 1000, 250);
// When the auto reset executes, change to every 1/2 second
reset.WaitOne(5000, false);
stateTimer.Change(0, 500);
Console.WriteLine("\nChanging the timer period.\n");
reset.WaitOne(5000, false);
stateTimer.Dispose();
Console.WriteLine("\nDestroying the timer.");
}
}
class StatusChecker
{
int invokeCount, maxCount;
public StatusChecker(int count)
{
invokeCount = 0;
maxCount = count;
}
// This method is called by the timer delegate.
public void CheckStatus(Object stateInfo)
{
AutoResetEvent autoEvent = (AutoResetEvent)stateInfo;
Console.WriteLine("{0} Checking status {1,2}.",
DateTime.Now.ToString("h:mm:ss.fff"),
(++invokeCount).ToString());
if (invokeCount == maxCount)
{
// Reset the counter and signal Main.
invokeCount = 0;
autoEvent.Set();
}
}
}
using System;
using System.Timers;
public class Program
{
private static System.Timers.Timer testTimer;
public static void Main(string[] args)
{
testTimer = new System.Timers.Timer(5000); // 5 seconds
testTimer.Elapsed += new ElapsedEventHandler(OnTimerElapsed);
testTimer.Interval = 5000;
testTimer.Enabled = true;
Console.WriteLine("Press the enter key to stop the timer");
Console.ReadLine();
}
private static void OnTimerElapsed(object source, ElapsedEventArgs e)
{
Console.WriteLine("Timer elapsed at {0}", e.SignalTime);
}
}
using System;
using System.Windows.Forms;
namespace WindowsFormsTimer
{
public class Class1
{
static System.Windows.Forms.Timer theTimer = new System.Windows.Forms.Timer();
static int alarmCounter = 1;
static bool exitFlag = false;
// Timer raised method
private static void TimerEventProcessor(Object myObject, EventArgs myEventArgs)
{
theTimer.Stop();
// Displays a message box asking whether to continue running the timer
if (MessageBox.Show("Continue running?", "Count is " + alarmCounter, MessageBoxButtons.YesNo) == DialogResult.Yes)
{
// Restarts the timer and increments the counter
alarmCounter += 1;
theTimer.Enabled = true;
}
else
{
// Stops the timer
exitFlag = true;
}
}
public static int Main()
{
//Adds the event and the event handler for the method that will
//process the timer event to the timer
theTimer.Tick += new EventHandler(TimerEventProcessor);
// Sets the timer interval to 5 seconds
theTimer.Interval = 2000;
theTimer.Start();
// Runs the timer, and raises the event
while (exitFlag == false)
{
// Processes all the events in the queue
Application.DoEvents();
}
return 0;
}
}
}
@cronywalls
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment