WPF定时器

DispatcherTimer定时器,可以说是专门为WPF界面设计的定时器。因为这个定时器是和UI都在同一线程上的。

添加命名空间引用

cs 复制代码
using System.Windows.Threading;

实例化

cs 复制代码
DispatcherTimer _timer = new DispatcherTimer();

设定定时器参数

cs 复制代码
 /*设置定时时间方法一*/
 _timer.Interval = new TimeSpan(0,1,0);//设置时间间隔h,m,s

 /*设置定时时间方法二*/
 timer.Interval = TimeSpan.FromMilliseconds(100);//设置的间隔为100ms

 _timer.Tick += Timer_Tick;//绑定计时事件(Tick事件在UI线程执行)

 _timer.DispatcherPriority = DispatcherPriority.Normal;//可选:设置定时器优先级(默认Normal)

启动、停止、修改定时时间

cs 复制代码
// 重启定时器
_timer.Start();

// 暂停定时器
_timer.Stop();

// 修改时间间隔(需先停止再修改,或直接修改后生效)
_timer.Interval = TimeSpan.FromMilliseconds(500); // 改为500ms触发一次
Tick 事件处理(更新 UI)
cs 复制代码
private void Timer_Tick(object sender, EventArgs e)
{
    _count++;
    // 直接更新UI控件(安全,因为在UI线程)
    lblTimer.Content = $"计时:{_count}秒";
    
    // 示例:计时到10秒停止
    if (_count >= 10)
    {
        _timer.Stop();
        lblTimer.Content = "计时结束!";
    }
}

实例

cs 复制代码
using System.Windows;
using System.Windows.Threading;

public partial class MainWindow : Window
{
    private DispatcherTimer _timer;
    private int _count = 0;

    public MainWindow()
    {
        InitializeComponent();
        
        // 1. 创建DispatcherTimer实例
        _timer = new DispatcherTimer();
        
        // 2. 设置时间间隔(单位:毫秒)
        _timer.Interval = TimeSpan.FromSeconds(1); // 每秒触发一次
        
        // 3. 绑定计时事件(Tick事件在UI线程执行)
        _timer.Tick += Timer_Tick;
        
        // 4. 可选:设置定时器优先级(默认Normal)
        _timer.DispatcherPriority = DispatcherPriority.Normal;
        
        // 5. 启动定时器
        _timer.Start();
    }
}
Tick 事件处理(更新 UI)
cs 复制代码
private void Timer_Tick(object sender, EventArgs e)
{
    _count++;
    // 直接更新UI控件(安全,因为在UI线程)
    lblTimer.Content = $"计时:{_count}秒";
    
    // 示例:计时到10秒停止
    if (_count >= 10)
    {
        _timer.Stop();
        lblTimer.Content = "计时结束!";
    }
}

label显示定时数据

复制代码
<!-- 用于数字自增显示 -->
<Label x:Name="counterLabel"
        Content="0"
        HorizontalAlignment="Left"
        VerticalAlignment="Top"
        Margin="20,20,0,0"
        FontSize="20"
        Padding="6"/>

private void timer_Tick(object? sender, EventArgs e)
{
    // 每次 Tick 自增并显示计数
    cnt++;
    Debug.WriteLine($"[{DateTime.Now:HH:mm:ss}] DispatcherTimer Tick 触发。cnt={cnt}");

    // DispatcherTimer 在 UI 线程运行,可以直接更新控件;使用 null-条件以防控件未命名或未初始化
    counterLabel?.SetValue(ContentControl.ContentProperty, cnt);
}
相关推荐
FreakStudio13 小时前
W55MH32L-EVB 上手测评:硬件 TCP/IP 加持的以太网单片机,MicroPython 零门槛开发
python·单片机·嵌入式·大学生·面向对象·并行计算·电子diy·电子计算机
✎ ﹏梦醒͜ღ҉繁华落℘6 天前
单片机基础知识---stm32单片机的优先级
stm32·单片机·mongodb
zd8451015006 天前
RS485 总线详解
单片机·嵌入式硬件
牛根生同志6 天前
SPI数据收发的时候 TXE与RXNE标志位置位的时机
stm32·spi·transfer
goldenrolan6 天前
学习型红外控制系统稳定性挂测工装专项总结
软件测试·python·stm32·嵌入式·红外
✎ ﹏梦醒͜ღ҉繁华落℘6 天前
编程基础 --高内聚,低耦合
c语言·单片机
科芯创展6 天前
1A,1MHz,30VIN,XZ4115,降压恒流LED驱动芯片
单片机·嵌入式硬件
集芯微电科技有限公司6 天前
四通道2A输出集成功率电感降压模块专为紧凑型方案设计
人工智能·单片机·嵌入式硬件·生成对抗网络·计算机外设
踏着七彩祥云的小丑6 天前
嵌入式测试学习第 37 天:异常场景测试:断电、拔插、干扰、非法指令
单片机·嵌入式硬件·学习
CC城子6 天前
STM32H7_FDCAN 驱动笔记
stm32·can·canfd