C# DateTime简单的定时器用法
时间: 2023-07-12 20:17:42 浏览: 59
C# 中使用 `System.Timers.Timer` 类可以实现简单的定时器功能。下面是一个示例代码:
```csharp
using System;
using System.Timers;
class Program
{
static void Main(string[] args)
{
Timer timer = new Timer(1000); // 设置定时器间隔为 1 秒
timer.Elapsed += Timer_Elapsed; // 添加定时器事件处理函数
timer.Enabled = true; // 启动定时器
Console.WriteLine("按任意键结束程序...");
Console.ReadKey();
}
private static void Timer_Elapsed(object sender, ElapsedEventArgs e)
{
Console.WriteLine("定时器触发时间:" + e.SignalTime);
}
}
```
以上代码中,定时器的间隔为 1 秒,每秒钟会触发一次 `Timer_Elapsed` 函数,输出当前时间。程序会一直运行,直到按下任意键结束程序。
相关问题
c# 定时器的几种方法
在 C# 中,你可以使用以下几种方法来实现定时器功能:
1. System.Timers.Timer:这是一个基于线程的定时器,可以在指定的时间间隔内重复执行指定的代码。你可以设置 Interval 属性来指定时间间隔,并通过 Elapsed 事件来处理计时器事件。
```csharp
using System;
using System.Timers;
public class TimerExample
{
private static Timer timer;
public static void Main()
{
timer = new Timer();
timer.Interval = 1000; // 设置时间间隔为1秒
timer.Elapsed += TimerElapsed; // 绑定事件处理程序
timer.Enabled = true; // 启动计时器
Console.WriteLine("Press Enter to stop the timer.");
Console.ReadLine();
timer.Enabled = false; // 停止计时器
}
private static void TimerElapsed(object sender, ElapsedEventArgs e)
{
Console.WriteLine("Timer elapsed at {0}", e.SignalTime);
}
}
```
2. System.Threading.Timer:这是一个基于线程池的定时器,也可以在指定的时间间隔内重复执行指定的代码。你可以使用 Change 方法来设置时间间隔,并通过 TimerCallback 委托来处理计时器事件。
```csharp
using System;
using System.Threading;
public class TimerExample
{
private static Timer timer;
public static void Main()
{
TimerCallback callback = new TimerCallback(TimerElapsed);
timer = new Timer(callback, null, 0, 1000); // 设置时间间隔为1秒
Console.WriteLine("Press Enter to stop the timer.");
Console.ReadLine();
timer.Dispose(); // 停止计时器
}
private static void TimerElapsed(object state)
{
Console.WriteLine("Timer elapsed at {0}", DateTime.Now);
}
}
```
3. System.Threading.Tasks.Task.Delay:这是一种基于异步任务的延迟执行方法,可以在指定的时间间隔后执行指定的代码。
```csharp
using System;
using System.Threading.Tasks;
public class TimerExample
{
public static async Task Main()
{
Console.WriteLine("Press Enter to start the timer.");
Console.ReadLine();
while (true)
{
Console.WriteLine("Timer elapsed at {0}", DateTime.Now);
await Task.Delay(1000); // 设置时间间隔为1秒
}
}
}
```
这些是在 C# 中实现定时器功能的几种常用方法。你可以根据自己的需求选择适合的方法来实现定时器功能。
C#定时器计公里标
您可以使用C#中的Timer类来实现计算公里数。首先,您需要记录开始时间和结束时间,并计算它们之间的时间差。然后,您可以使用已知的速度计算行驶的距离(公里)。以下是一个简单的示例代码:
```csharp
using System;
using System.Timers;
class Program
{
static DateTime startTime;
static DateTime endTime;
static double speed = 60.0; // km/h
static void Main(string[] args)
{
Console.WriteLine("Press any key to start the timer...");
Console.ReadKey();
startTime = DateTime.Now;
Timer timer = new Timer(1000);
timer.Elapsed += OnTimedEvent;
timer.Enabled = true;
Console.WriteLine("Press any key to stop the timer...");
Console.ReadKey();
endTime = DateTime.Now;
double distance = speed * (endTime - startTime).TotalHours;
Console.WriteLine("Distance traveled: {0:F2} km", distance);
}
private static void OnTimedEvent(Object source, ElapsedEventArgs e)
{
TimeSpan timeElapsed = e.SignalTime - startTime;
Console.WriteLine("Time elapsed: {0}", timeElapsed.ToString(@"hh\:mm\:ss"));
}
}
```
在这个示例中,我们使用了一个Timer对象来每秒钟触发一次OnTimedEvent方法。在开始计时时,我们记录了当前时间并在每次定时器事件发生时计算经过的时间。在停止计时时,我们计算速度和距离,并将其显示在控制台上。