home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 40 / IOPROG_40.ISO / SOFT / NETFrameworkSDK.exe / comsdk.cab / samples.exe / QuickStart / howto / doc / timer.aspx < prev    next >
Encoding:
Text File  |  2000-06-07  |  3.8 KB  |  84 lines

  1.  
  2. <!-- #include virtual="/quickstart/howto/include/header.inc" -->
  3.  
  4. <h4>How Do I...Get timer events without message pump?</h4>
  5.  
  6. <div class="indent" style="width:660">
  7. <p class="MsoNormal"><font face="Tahoma" size="1">Timer allows you to raise an
  8. event on a specified interval. </font></p>
  9. <p class="MsoNormal"><font face="Tahoma" size="1">This sample illustrates how to
  10. use a Timer to raise an event every 1000 milliseconds. Run the sample and
  11. observe a new "Hello World" message being printed every second.</font></p>
  12. <p class="MsoNormal"><font face="Tahoma" size="1">In its simplest form, using a
  13. Timer involves:</font></p>
  14. <p class="MsoNormal"><font face="Tahoma" size="1">1. Creating a new instance of
  15. Timer:</font></p>
  16. <blockquote>
  17.   <p class="MsoNormal"><span style="mso-bidi-font-size: 12.0pt"><font color="#0000FF" face="Courier New" size="2">System.Timers.Timer
  18.   aTimer = new System.Timers.Timer();</font><font face="Tahoma" size="1"><o:p>
  19.   </o:p>
  20.   </font></span></p>
  21. </blockquote>
  22. <p><font face="Tahoma" size="1">2. </font><font face="Tahoma" size="1">Specifying
  23. the event handler:</font></p>
  24. <blockquote>
  25.   <p class="MsoNormal"><span style="mso-bidi-font-size: 12.0pt"><font face="Courier New" size="2" color="#0000FF">aTimer.AddOnTimer(new
  26.   EventHandler(OnTimer));</font><font face="Tahoma" size="1"><o:p>
  27.   </o:p>
  28.   </font></span></p>
  29. </blockquote>
  30. <p class="MsoNormal"><font face="Tahoma" size="1">3. </font><font face="Tahoma" size="1">Specifying
  31. how often to raise the event:</font></p>
  32. <blockquote>
  33.   <p class="MsoNormal"><span style="mso-bidi-font-size: 12.0pt"><font face="Courier New" size="2" color="#0000FF">aTimer.Interval
  34.   = 1000;</font><font face="Tahoma" size="1"><o:p>
  35.   </o:p>
  36.   </font></span></p>
  37. </blockquote>
  38. <p class="MsoNormal"><font face="Tahoma" size="1">4. Enabling the component:</font></p>
  39. <blockquote>
  40.   <p class="MsoNormal"><span style="mso-bidi-font-size: 12.0pt"><font color="#0000FF" face="Courier New" size="2">aTimer.Enabled
  41.   = true;</font><font face="Tahoma" size="1"><o:p>
  42.   </o:p>
  43.   </font></span></p>
  44. </blockquote>
  45. <p><font face="Tahoma" size="1">5. Handling the event:</font></p>
  46. <blockquote>
  47.   <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
  48.   static void OnTimer(Object source, EventArgs e)<o:p>
  49.   </o:p>
  50.   <span style="mso-tab-count:1"> </span>{<o:p>
  51.   </o:p>
  52.   </span></font></p>
  53.   <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">
  54.   </span><span style="mso-bidi-font-size: 12.0pt">Console.WriteLine("Hello
  55.   World!");<o:p>
  56.   </o:p>
  57.   </span></font></p>
  58.   <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>
  59.   </o:p>
  60.   </span></p>
  61. </blockquote>
  62. </div>
  63.  
  64. <h4>Example</h4>
  65.  
  66. <p>
  67. <div class="indent">
  68. <a target="_blank" href="/quickstart/howto/samples/Services/Timers/Timer">
  69. <img style="border-color:black" border=1 src="/quickstart/images/genicon.gif"><br>
  70. </a>
  71. <div class="caption">Timer.exe</div><br>
  72. [<a target="_blank" href="/quickstart/howto/samples/Services/Timers/Timer">View Sample</a>] | 
  73. [<a target="_blank" href="/quickstart/util/srcview.aspx?path=/quickstart/howto/samples/Services/Timers/Timer/Timer.src">View Source</a>]<p>
  74. </div>
  75.  
  76. <h4>Source Code</h4>
  77.  
  78. <div class="code">
  79. <xmp>
  80. <!-- #include virtual="/quickstart/howto/samples/Services/Timers/Timer/Timer.cs" -->
  81. </xmp>
  82. </div>
  83.  
  84. <!-- #include virtual="/quickstart/howto/include/footer.inc" -->