home *** CD-ROM | disk | FTP | other *** search
-
- <!-- #include virtual="/quickstart/howto/include/header.inc" -->
-
- <h4>How Do I...Get timer events without message pump?</h4>
-
- <div class="indent" style="width:660">
- <p class="MsoNormal"><font face="Tahoma" size="1">Timer allows you to raise an
- event on a specified interval. </font></p>
- <p class="MsoNormal"><font face="Tahoma" size="1">This sample illustrates how to
- use a Timer to raise an event every 1000 milliseconds. Run the sample and
- observe a new "Hello World" message being printed every second.</font></p>
- <p class="MsoNormal"><font face="Tahoma" size="1">In its simplest form, using a
- Timer involves:</font></p>
- <p class="MsoNormal"><font face="Tahoma" size="1">1. Creating a new instance of
- Timer:</font></p>
- <blockquote>
- <p class="MsoNormal"><span style="mso-bidi-font-size: 12.0pt"><font color="#0000FF" face="Courier New" size="2">System.Timers.Timer
- aTimer = new System.Timers.Timer();</font><font face="Tahoma" size="1"><o:p>
- </o:p>
- </font></span></p>
- </blockquote>
- <p><font face="Tahoma" size="1">2. </font><font face="Tahoma" size="1">Specifying
- the event handler:</font></p>
- <blockquote>
- <p class="MsoNormal"><span style="mso-bidi-font-size: 12.0pt"><font face="Courier New" size="2" color="#0000FF">aTimer.AddOnTimer(new
- EventHandler(OnTimer));</font><font face="Tahoma" size="1"><o:p>
- </o:p>
- </font></span></p>
- </blockquote>
- <p class="MsoNormal"><font face="Tahoma" size="1">3. </font><font face="Tahoma" size="1">Specifying
- how often to raise the event:</font></p>
- <blockquote>
- <p class="MsoNormal"><span style="mso-bidi-font-size: 12.0pt"><font face="Courier New" size="2" color="#0000FF">aTimer.Interval
- = 1000;</font><font face="Tahoma" size="1"><o:p>
- </o:p>
- </font></span></p>
- </blockquote>
- <p class="MsoNormal"><font face="Tahoma" size="1">4. Enabling the component:</font></p>
- <blockquote>
- <p class="MsoNormal"><span style="mso-bidi-font-size: 12.0pt"><font color="#0000FF" face="Courier New" size="2">aTimer.Enabled
- = true;</font><font face="Tahoma" size="1"><o:p>
- </o:p>
- </font></span></p>
- </blockquote>
- <p><font face="Tahoma" size="1">5. Handling the event:</font></p>
- <blockquote>
- <p class="MsoNormal" style="word-spacing: 0; margin-top: 0; margin-bottom: 0"><font color="#0000FF" face="Courier New" size="2"><span style="mso-bidi-font-size: 12.0pt">public
- static void OnTimer(Object source, EventArgs e)<o:p>
- </o:p>
- <span style="mso-tab-count:1"> </span>{<o:p>
- </o:p>
- </span></font></p>
- <p class="MsoNormal" style="word-spacing: 0; margin-top: 0; margin-bottom: 0"> <font color="#0000FF" face="Courier New" size="2"><span style="mso-tab-count: 1; mso-bidi-font-size: 12.0pt">
- </span><span style="mso-bidi-font-size: 12.0pt">Console.WriteLine("Hello
- World!");<o:p>
- </o:p>
- </span></font></p>
- <p class="MsoNormal" style="word-spacing: 0; margin-top: 0; margin-bottom: 0"><span style="mso-bidi-font-size: 12.0pt"><font color="#0000FF" face="Courier New" size="2">}</font></span><span style="font-size:8.0pt;mso-bidi-font-size:12.0pt"><o:p>
- </o:p>
- </span></p>
- </blockquote>
- </div>
-
- <h4>Example</h4>
-
- <p>
- <div class="indent">
- <a target="_blank" href="/quickstart/howto/samples/Services/Timers/Timer">
- <img style="border-color:black" border=1 src="/quickstart/images/genicon.gif"><br>
- </a>
- <div class="caption">Timer.exe</div><br>
- [<a target="_blank" href="/quickstart/howto/samples/Services/Timers/Timer">View Sample</a>] |
- [<a target="_blank" href="/quickstart/util/srcview.aspx?path=/quickstart/howto/samples/Services/Timers/Timer/Timer.src">View Source</a>]<p>
- </div>
-
- <h4>Source Code</h4>
-
- <div class="code">
- <xmp>
- <!-- #include virtual="/quickstart/howto/samples/Services/Timers/Timer/Timer.cs" -->
- </xmp>
- </div>
-
- <!-- #include virtual="/quickstart/howto/include/footer.inc" -->