<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Martijn's C# Programming Blog &#187; System.Timers</title>
	<atom:link href="http://www.dijksterhuis.org/tag/systemtimers/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dijksterhuis.org</link>
	<description>Information, news about programming in C#</description>
	<lastBuildDate>Fri, 07 Aug 2009 21:26:47 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Using Timers in C#</title>
		<link>http://www.dijksterhuis.org/using-timers-in-c/</link>
		<comments>http://www.dijksterhuis.org/using-timers-in-c/#comments</comments>
		<pubDate>Fri, 21 Nov 2008 09:10:12 +0000</pubDate>
		<dc:creator>Martijn</dc:creator>
				<category><![CDATA[Beginner]]></category>
		<category><![CDATA[Learn C#]]></category>
		<category><![CDATA[keypressed]]></category>
		<category><![CDATA[System.Timers]]></category>

		<guid isPermaLink="false">http://www.dijksterhuis.org/?p=237</guid>
		<description><![CDATA[
Sometimes its handy to set an alarm clock &#8212; to tell you when to get that next cup of coffee for example. Your C# programs also might want to check that everything is going smoothly. Timers are handy if you want to check for example whether that download has stalled, a message needs to be [...]<p>This is a post from <a href="http://www.dijksterhuis.org">Martijn's C# Coding Blog</a>. </p>
]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.dijksterhuis.org/wp-content/uploads/2008/11/timer.jpg" alt="" title="timer" width="500" height="283" class="alignnone size-full wp-image-241" /></p>
<p><em>Sometimes its handy to set an alarm clock &#8212; to tell you when to get that next cup of coffee for example. Your C# programs also might want to check that everything is going smoothly. Timers are handy if you want to check for example whether that download has stalled, a message needs to be send or a file needs to be updated.</em></p>
<p><span id="more-237"></span></p>
<p>Creating a timer in C# is very simple. You need to create an instance of System.Timers.Timer (which in this case we need to spell out, as it has a name conflict with System.Threading.Timer) and assign a callback function. The callback function is called each time the timer event is fired, there is no need to reset the timer as the system will continue to call it automatically.</p>
<p>The following example sets a timer that prints the time of its firing roughly every second. Note that the timing of each event is not exact. After the set period has expired C# queues a ThreadPool task which will then try to find an available thread to actually execute the callback.</p>
<p>We need to assign the callback to the &#8220;Elapsed&#8221; event property. If we also would like to fire an event when the Timer is disposed off, we can attach an event to disposed &#8212; but this is optional. </p>
<pre class="brush: c#">
using System;
using System.Timers;
using System.Threading;

public class TimerExample
{
    // Our timer
    private static System.Timers.Timer TheTimer;

    public static void Main()
    {
        TheTimer = new System.Timers.Timer();

        TheTimer.Elapsed += new ElapsedEventHandler(OurTimerCallback);
        TheTimer.Disposed += new EventHandler(OurTimerDisposed);
        TheTimer.Interval = 1000;
        TheTimer.Enabled = true;

        Console.WriteLine(&quot;Press &#039;x&#039; to quit&quot;);
        CheckForExitKey();

		// Allow the timer to clean up
		TheTimer.Dispose();

        Console.WriteLine(&quot;Main exits.&quot;);
    }

    public static void OurTimerCallback(object source, ElapsedEventArgs e)
    {
        Console.WriteLine(&quot;Received a callback, the time is {0}&quot;, e.SignalTime);
    }

    public static void OurTimerDisposed(object source, EventArgs e)
    {
        Console.WriteLine(&quot;Called when the timer is disposed off&quot;);
    }

    public static void CheckForExitKey()
    {
        ConsoleKeyInfo cki = new ConsoleKeyInfo();
        while (true)
        {
           while (Console.KeyAvailable == false)
            Thread.Sleep(250); // We poll -- since no event exists, wait for 250ms
           cki = Console.ReadKey(true);
           if (cki.Key == ConsoleKey.X) break;
        }
    }

}
</pre>
<p>This is a post from <a href="http://www.dijksterhuis.org">Martijn's C# Coding Blog</a>. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.dijksterhuis.org/using-timers-in-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
