C#定时器

本文介绍了C#中的三种定时器:System.Windows.Forms.Timer、System.Threading.Timer和System.Timers.Timer。System.Windows.Forms.Timer适用于WinForm,依赖Windows消息机制,计时不精确。System.Threading.Timer和System.Timers.Timer基于.NET ThreadPool,计时精确,适用于更多场景。文中通过实例展示了如何使用这三种定时器,并探讨了它们各自的优缺点和适用范围。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

关于C#中timer类  在C#里关于定时器类就有3个   
1.定义在System.Windows.Forms里   
2.定义在System.Threading.Timer类里   
3.定义在System.Timers.Timer类里  

System.Windows.Forms.Timer是应用于WinForm中的,他是通过Windows消息机制实现的,类似于VB或Delphi中的Timer控件,内部使用API  SetTimer实现的。他的主要缺点是计时不精确,而且必须有消息循环,Console  Application(控制台应用程式)无法使用。   
  
System.Timers.Timer和System.Threading.Timer很类似,他们是通过.NET  Thread  Pool实现的,轻量,计时精确,对应用程式、消息没有特别的需要。System.Timers.Timer还能够应用于WinForm,完全取代上面的Timer控件。他们的缺点是不支持直接的拖放,需要手工编码。 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Timers;
using System.Collections;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

  

       
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值