翼度科技»论坛 编程开发 .net 查看内容

探索.NET中的定时器:选择最适合你的应用场景

5

主题

5

帖子

15

积分

新手上路

Rank: 1

积分
15
 
概述:.NET提供多种定时器,如
System.Windows.Forms.Timer适用于UI,System.Web.UI.Timer用于Web,System.Diagnostics.Timer用于性能监控,System.Threading.Timer和System.Timers.Timer用于一般定时任务。在.NET 6及以上,还有更直观的System.Threading.PeriodicTimer。选择合适的定时器,提升应用性能和用户体验。
今天看到一网友聊.net中的定时器,我也把我知道和大家分享一下。 在.NET中,有多种定时器的实现,每一种都有其特定的应用场景和特点。下面将分别介绍这几种定时器,并提供相应的实例源代码。
1. System.Windows.Forms.Timer

应用场景: 适用于Windows Forms应用程序中需要与UI线程交互的场景。
特点: 在UI线程中工作,可以直接访问和操作UI控件。
实例:
  1. using System;
  2. using System.Windows.Forms;
  3. public class MainForm : Form
  4. {
  5.     private Timer timer;
  6.     public MainForm()
  7.     {
  8.         // 创建定时器,每秒触发一次
  9.         timer = new Timer();
  10.         timer.Interval = 1000;
  11.         timer.Tick += TimerTick;
  12.         // 启动定时器
  13.         timer.Start();
  14.     }
  15.     private void TimerTick(object sender, EventArgs e)
  16.     {
  17.         // 在UI线程中更新UI
  18.         label1.Text = "定时器触发时间:" + DateTime.Now;
  19.     }
  20.     static void Main()
  21.     {
  22.         Application.Run(new MainForm());
  23.     }
  24. }
复制代码
2. System.Windows.Threading.DispatcherTimer

应用场景: 适用于WPF应用程序中需要与UI线程交互的场景。
特点: 基于WPF的 Dispatcher,可直接访问和操作UI控件。
实例:
  1. using System;
  2. using System.Windows;
  3. using System.Windows.Threading;
  4. public partial class MainWindow : Window
  5. {
  6.     private DispatcherTimer timer;
  7.     public MainWindow()
  8.     {
  9.         // 创建定时器,每秒触发一次
  10.         timer = new DispatcherTimer();
  11.         timer.Interval = TimeSpan.FromSeconds(1);
  12.         timer.Tick += TimerTick;
  13.         // 启动定时器
  14.         timer.Start();
  15.     }
  16.     private void TimerTick(object sender, EventArgs e)
  17.     {
  18.         // 在UI线程中更新UI
  19.         label1.Content = "定时器触发时间:" + DateTime.Now.ToString("HH:mm:ss");
  20.     }
  21.     static void Main()
  22.     {
  23.         Application app = new Application();
  24.         app.Run(new MainWindow());
  25.     }
  26. }
复制代码
3. System.Web.UI.Timer

应用场景: 适用于ASP.NET Web应用程序中,用于在Web页面上执行定时操作。
特点: 在Web页面的服务器端运行,适用于Web Forms。
实例:
ASP.NET Web Forms中在aspx页面的代码:
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
  2. <!DOCTYPE html>
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5.     <title></title>
  6. </head>
  7. <body>
  8.     <form id="form1" runat="server">
  9.         <asp:ScriptManager runat="server" />
  10.         <asp:UpdatePanel runat="server">
  11.             <ContentTemplate>
  12.                 <asp:Label ID="Label1" runat="server" Text="定时器触发时间:" />
  13.                 <asp:Timer ID="Timer1" runat="server" Interval="1000" OnTick="TimerTick"></asp:Timer>
  14.             </ContentTemplate>
  15.         </asp:UpdatePanel>
  16.     </form>
  17. </body>
  18. </html>
复制代码
对应的代码文件 Default.aspx.cs:
  1. using System;
  2. public partial class _Default : System.Web.UI.Page
  3. {
  4.     protected void TimerTick(object sender, EventArgs e)
  5.     {
  6.         // 在服务器端更新UI
  7.         Label1.Text = "定时器触发时间:" + DateTime.Now.ToString("HH:mm:ss");
  8.     }
  9. }
复制代码
4. System.Diagnostics.Timer

应用场景: 适用于性能计数器的定时器,用于性能监控和测量。
特点: 基于性能计数器的定时器。
实例:
  1. using System;
  2. using System.Diagnostics;
  3. class Program
  4. {
  5.     static void Main()
  6.     {
  7.         // 创建定时器,每秒触发一次
  8.         Timer timer = new Timer(1000);
  9.         
  10.         // 定时器触发事件
  11.         timer.Elapsed += TimerElapsed;
  12.         // 启动定时器
  13.         timer.Start();
  14.         // 阻止程序退出
  15.         Console.ReadLine();
  16.     }
  17.     static void TimerElapsed(object sender, ElapsedEventArgs e)
  18.     {
  19.         Console.WriteLine("定时器触发时间:" + e.SignalTime);
  20.     }
  21. }
复制代码
5. System.Timers.Timer

应用场景: 适用于需要在一定时间间隔内重复执行任务的场景,如定时数据采集、日志记录等。
特点: 基于事件触发机制,可在多线程环境中使用,但注意处理线程同步。
实例:
  1. using System;
  2. using System.Timers;
  3. class Program
  4. {
  5.     static void Main()
  6.     {
  7.         // 创建定时器,每秒触发一次
  8.         Timer timer = new Timer(1000);
  9.         // 定时器触发事件
  10.         timer.Elapsed += TimerElapsed;
  11.         // 启动定时器
  12.         timer.Start();
  13.         // 阻止程序退出
  14.         Console.ReadLine();
  15.     }
  16.     static void TimerElapsed(object sender, ElapsedEventArgs e)
  17.     {
  18.         Console.WriteLine("定时器触发时间:" + e.SignalTime);
  19.     }
  20. }
复制代码
6. System.Threading.Timer

应用场景: 适用于需要在一定时间间隔内执行任务,但不需要与UI线程交互的场景,如后台任务的调度。
特点: 基于线程池,不依赖于UI线程,需要注意线程同步和异常处理。
实例:
  1. using System;
  2. using System.Threading;
  3. class Program
  4. {
  5.     static void Main()
  6.     {
  7.         // 创建定时器,每秒触发一次
  8.         Timer timer = new Timer(TimerCallback, null, 0, 1000);
  9.         // 阻止程序退出
  10.         Console.ReadLine();
  11.     }
  12.     static void TimerCallback(object state)
  13.     {
  14.         Console.WriteLine("定时器触发时间:" + DateTime.Now);
  15.     }
  16. }
复制代码
7. System.Threading.PeriodicTimer (需要.NET 6及以上版本)

应用场景: 适用于需要定期执行任务的场景,替代 System.Threading.Timer。
特点: .NET 6及以上版本引入的新型定时器,提供更直观的API和更稳定的性能。
实例:
  1. using System;
  2. using System.Threading;
  3. class Program
  4. {
  5.     static void Main()
  6.     {
  7.         // 创建定时器,每秒触发一次
  8.         using (PeriodicTimer timer = new PeriodicTimer(TimeSpan.FromSeconds(1)))
  9.         {
  10.             // 定时器触发事件
  11.             timer.Elapsed += TimerElapsed;
  12.             // 启动定时器
  13.             timer.Start();
  14.             // 阻止程序退出
  15.             Console.ReadLine();
  16.         }
  17.     }
  18.     static void TimerElapsed(object sender, EventArgs e)
  19.     {
  20.         Console.WriteLine("定时器触发时间:" + DateTime.Now);
  21.     }
  22. }
复制代码
这些定时器各自适用于不同的场景,选择合适的定时器取决于你的具体需求和应用程序类型。
在使用定时器时,请注意处理好线程同步、资源释放等问题,以确保应用程序的稳定性和性能。
 



来源:https://www.cnblogs.com/hanbing81868164/p/18038888
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具