本文整理汇总了C#中System.Timers.Timer.Interval属性的典型用法代码示例。如果您正苦于以下问题:C# Timer.Interval属性的具体用法?C# Timer.Interval怎么用?C# Timer.Interval使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类System.Timers.Timer
的用法示例。
在下文中一共展示了Timer.Interval属性的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
using System;
using System.Timers;
public class Example
{
private static Timer aTimer;
public static void Main()
{
// Create a timer and set a two second interval.
aTimer = new System.Timers.Timer();
aTimer.Interval = 2000;
// Hook up the Elapsed event for the timer.
aTimer.Elapsed += OnTimedEvent;
// Have the timer fire repeated events (true is the default)
aTimer.AutoReset = true;
// Start the timer
aTimer.Enabled = true;
Console.WriteLine("Press the Enter key to exit the program at any time... ");
Console.ReadLine();
}
private static void OnTimedEvent(Object source, System.Timers.ElapsedEventArgs e)
{
Console.WriteLine("The Elapsed event was raised at {0}", e.SignalTime);
}
}
输出:
Press the Enter key to exit the program at any time... The Elapsed event was raised at 5/20/2015 8:48:58 PM The Elapsed event was raised at 5/20/2015 8:49:00 PM The Elapsed event was raised at 5/20/2015 8:49:02 PM The Elapsed event was raised at 5/20/2015 8:49:04 PM The Elapsed event was raised at 5/20/2015 8:49:06 PM
示例2: Main
//引入命名空间
using System;
using System.IO;
using System.Collections.Generic;
using System.Timers;
public class Example
{
private static Timer aTimer;
private static List<String> eventlog;
private static int nEventsFired = 0;
private static DateTime previousTime;
public static void Main()
{
eventlog = new List<String>();
StreamWriter sr = new StreamWriter(@".\Interval.txt");
// Create a timer with a five millisecond interval.
aTimer = new Timer(5);
aTimer.Elapsed += OnTimedEvent;
// Hook up the Elapsed event for the timer.
aTimer.AutoReset = true;
sr.WriteLine("The timer should fire every {0} milliseconds.",
aTimer.Interval);
aTimer.Enabled = true;
Console.WriteLine("Press the Enter key to exit the program... ");
Console.ReadLine();
foreach (var item in eventlog)
sr.WriteLine(item);
sr.Close();
Console.WriteLine("Terminating the application...");
}
private static void OnTimedEvent(Object source, ElapsedEventArgs e)
{
eventlog.Add(String.Format("Elapsed event at {0:HH':'mm':'ss.ffffff} ({1})",
e.SignalTime,
nEventsFired++ == 0 ?
0.0 : (e.SignalTime - previousTime).TotalMilliseconds));
previousTime = e.SignalTime;
if (nEventsFired == 20) {
Console.WriteLine("No more events will fire...");
aTimer.Enabled = false;
}
}
}
// The example writes output like the following to a file:
// The timer should fire every 5 milliseconds.
// Elapsed event at 08:42:49.370344 (0)
// Elapsed event at 08:42:49.385345 (15.0015)
// Elapsed event at 08:42:49.400347 (15.0015)
// Elapsed event at 08:42:49.415348 (15.0015)
// Elapsed event at 08:42:49.430350 (15.0015)
// Elapsed event at 08:42:49.445351 (15.0015)
// Elapsed event at 08:42:49.465353 (20.002)
// Elapsed event at 08:42:49.480355 (15.0015)
// Elapsed event at 08:42:49.495356 (15.0015)
// Elapsed event at 08:42:49.510358 (15.0015)
// Elapsed event at 08:42:49.525359 (15.0015)
// Elapsed event at 08:42:49.540361 (15.0015)
// Elapsed event at 08:42:49.555362 (15.0015)
// Elapsed event at 08:42:49.570364 (15.0015)
// Elapsed event at 08:42:49.585365 (15.0015)
// Elapsed event at 08:42:49.605367 (20.002)
// Elapsed event at 08:42:49.620369 (15.0015)
// Elapsed event at 08:42:49.635370 (15.0015)
// Elapsed event at 08:42:49.650372 (15.0015)
// Elapsed event at 08:42:49.665373 (15.0015)
示例3: Main
//引入命名空间
using System;
using System.Runtime.InteropServices;
public class Example
{
[DllImport("kernel32.dll", SetLastError=true)]
static extern bool GetSystemTimeAdjustment(out long lpTimeAdjustment,
out long lpTimeIncrement,
out bool lpTimeAdjustmentDisabled);
public static void Main()
{
long timeAdjustment, timeIncrement = 0;
bool timeAdjustmentDisabled;
if (GetSystemTimeAdjustment(out timeAdjustment, out timeIncrement,
out timeAdjustmentDisabled)) {
if (! timeAdjustmentDisabled)
Console.WriteLine("System clock resolution: {0:N3} milliseconds",
timeIncrement/10000.0);
else
Console.WriteLine("Unable to determine system clock resolution.");
}
}
}
输出:
System clock resolution: 15.600 milliseconds
示例4: Main
//引入命名空间
using System;
using System.Drawing;
using System.Windows.Forms;
class DigitalClock: Form
{
public static void Main()
{
Application.Run(new DigitalClock());
}
public DigitalClock()
{
ResizeRedraw = true;
Timer timer = new Timer();
timer.Tick += new EventHandler(TimerOnTick);
timer.Interval = 1000;
timer.Start();
}
private void TimerOnTick(object obj, EventArgs ea)
{
Invalidate();
}
protected override void OnPaint(PaintEventArgs pea)
{
Graphics grfx = pea.Graphics;
DateTime dt = DateTime.Now;
string strTime = dt.ToString("d") + "\n" + dt.ToString("T");
SizeF sizef = grfx.MeasureString(strTime, Font);
float fScale = Math.Min(ClientSize.Width / sizef.Width,
ClientSize.Height / sizef.Height);
Font font = new Font(Font.FontFamily,
fScale * Font.SizeInPoints);
StringFormat strfmt = new StringFormat();
strfmt.Alignment = strfmt.LineAlignment = StringAlignment.Center;
grfx.DrawString(strTime, font, new SolidBrush(ForeColor),
ClientRectangle, strfmt);
}
}