C# 中Stopwatch和timer的实现示例
<div id="navCategory"><h5 class="catalogue">目录</h5><ul class="first_class_ul"><li>Stopwatch 类</li><li>Timer 类</li><ul class="second_class_ul"><li>System.Timers.Timer</li><li>System.Windows.Forms.Timer</li></ul><li>总结</li><ul class="second_class_ul"></ul></ul></div><p>在C#中,<code>Stopwatch</code> 和 <code>Timer</code>(通常指的是 <code>System.Timers.Timer</code> 或 <code>System.Windows.Forms.Timer</code>)是两个不同的类,它们用于不同的目的:</p><p class="maodian"></p><h2>Stopwatch 类</h2>
<p><code>Stopwatch</code> 类位于 <code>System.Diagnostics</code> 命名空间,主要用于精确测量时间间隔。它非常适合用于性能分析、测量代码块的执行时间或任何需要高精度计时的场景。<code>Stopwatch</code> 提供了以下功能:</p>
<ul><li>以高精度(通常为计时器分辨率,可能是微秒级别)启动、停止和重置计时器。</li><li>提供了 <code>Elapsed</code> 属性,返回一个 <code>TimeSpan</code> 对象,表示经过的时间。</li><li>支持跨平台使用,因为它不依赖于操作系统的计时器。</li></ul>
<p><strong>示例代码</strong>:</p>
<div class="jb51code"><pre class="brush:csharp;">using System;
using System.Diagnostics;
class Program
{
static void Main()
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
// 执行一些操作
for (int i = 0; i < 1000000; i++)
{
// 模拟工作负载
}
stopwatch.Stop();
Console.WriteLine("Elapsed time: " + stopwatch.Elapsed);
}
}</pre></div>
<p class="maodian"></p><h2>Timer 类</h2>
<p><code>Timer</code> 类通常指的是 <code>System.Timers.Timer</code> 或 <code>System.Windows.Forms.Timer</code>,它们用于在指定的时间间隔后执行代码。这些计时器主要用于定时任务,如每隔一段时间执行一次操作。</p>
<p class="maodian"></p><h3>System.Timers.Timer</h3>
<p><code>System.Timers.Timer</code> 位于 <code>System.Timers</code> 命名空间,提供了一个服务器端的定时器,可以用于任何需要定时执行任务的场景,包括Windows服务。</p>
<p><strong>示例代码</strong>:</p>
<div class="jb51code"><pre class="brush:csharp;">using System;
using System.Timers;
class Program
{
static void Main()
{
Timer timer = new Timer(1000); // 设置定时器间隔为1000毫秒
timer.Elapsed += (sender, e) =>
{
Console.WriteLine("Timer ticked at " + DateTime.Now);
// 执行定时任务
};
timer.AutoReset = true; // 设置定时器自动重置
timer.Enabled = true; // 启动定时器
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
timer.Dispose(); // 清理资源
}
}</pre></div>
<p class="maodian"></p><h3>System.Windows.Forms.Timer</h3>
<p><code>System.Windows.Forms.Timer</code> 位于 <code>System.Windows.Forms</code> 命名空间,主要用于Windows Forms应用程序中,以指定的时间间隔触发事件。</p>
<div class="jb51code"><pre class="brush:csharp;">using System;
using System.Windows.Forms;
class Program
{
static void Main()
{
Timer timer = new Timer();
timer.Interval = 1000; // 设置定时器间隔为1000毫秒
timer.Tick += (sender, e) =>
{
Console.WriteLine("Timer ticked at " + DateTime.Now);
// 执行定时任务
};
timer.Start(); // 启动定时器
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
timer.Stop(); // 停止定时器
}
}</pre></div>
<p class="maodian"></p><h2>总结</h2>
<ul><li><strong>Stopwatch</strong>:用于测量时间间隔,适合性能分析和精确计时。</li><li><strong>Timer</strong>:用于在指定的时间间隔后执行代码,适合定时任务。</li></ul>
<p>根据你的具体需求,可以选择使用 <code>Stopwatch</code> 来测量时间间隔,或使用 <code>Timer</code> 来执行定时任务。</p>
<p>到此这篇关于C# 中Stopwatch和timer的实现示例的文章就介绍到这了,更多相关C# Stopwatch timer内容请搜索琼殿技术社区以前的文章或继续浏览下面的相关文章希望大家以后多多支持琼殿技术社区!</p>
<div class="art_xg">
<b>您可能感兴趣的文章:</b><ul><li>C#中高精度计时器Stopwatch的用法详解</li><li>C#使用Stopwatch实现计时功能</li><li>C# Stopwatch实现计算代码运行时间</li><li>C#中Stopwatch的使用及说明</li><li>C# 中使用Stopwatch计时器实现暂停计时继续计时功能</li><li>如何使用C# Stopwatch 测量微秒级精确度</li><li>.NET/C# 使用Stopwatch测量运行时间</li><li>C#使用StopWatch获取程序毫秒级执行时间的方法</li><li>C#中Forms.Timer、Timers.Timer、Threading.Timer的用法分析</li><li>C#中的Timer和DispatcherTimer使用实例</li><li>C#中的三种定时计时器Timer用法介绍</li><li>C#中三种Timer计时器的详细用法</li><li>详解C#中的定时器Timer类及其垃圾回收机制</li><li>C#使用timer实现的简单闹钟程序</li><li>.NET中几种Timer的使用实例</li></ul>
</div>
</div>
<!--endmain-->
頁:
[1]