C#使用计时器

目录

1.Timer使用

2.System.Timers.Timer使用

3.System.Threading.Timer使用

4.DispatcherTimer使用

c#中计时器有4种:

  1. Timer timer = new Timer(),控件
  2. System.Timers.Timer timer2 = new System.Timers.Timer();代码
  3. System.Threading.Timer threadTimer = new System.Threading.Timer( );  代码
  4. DispatcherTimer dispatcherTimer = new DispatcherTimer();代码

winform中可以使用的是:123

WPF中可以使用的是:234

其中23都不依赖窗体

1.Timer使用

可以在winform中的工具栏中直接拖一个控件

也可以在代码中自己new一个

代码:

记得使用using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Timer timer = new Timer();
        public int a = 0;
        private void button1_Click(object sender, EventArgs e)
        {
            timer.Start();
            timer.Tick += Timer_Tick;
        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            a++;
            label1.Text = a.ToString();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            timer.Enabled = true;
            timer.Interval = 1000;
        }
    }
}

2.System.Timers.Timer使用

2种方式

第一种:使用SynchronizingObject,和上面的用法一样,单线程方式。

代码

记得使用using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        System.Timers.Timer timer  = new System.Timers.Timer();
        public int a = 0;
        private void button1_Click(object sender, EventArgs e)
        {
            timer.Start();
            timer.Elapsed += Timer_Elapsed;
        }

        private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            a++;
            label1.Text = a.ToString();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            timer.Enabled = true;
            timer.Interval = 1000;
            timer.SynchronizingObject = this;
        }
    }
}

第二种,不使用SynchronizingObject,多线程方式。

代码

记得使用using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        System.Timers.Timer timer  = new System.Timers.Timer();
        delegate void SetTextCallback(string text);             //委托
        public int a = 0;
        private void button1_Click(object sender, EventArgs e)
        {
            timer.Start();
            timer.Elapsed += Timer_Elapsed;
        }

        private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            a++;
            SetTextCallback deg = new SetTextCallback(SetText);
            this.Invoke(deg, new object[] { a.ToString() });   //委托传值
           
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            timer.Enabled = true;
            timer.Interval = 1000;
        }

        private void SetText(string text)
        {
            label1.Text = text;
        }
    }
}

3.System.Threading.Timer使用

代码

记得使用using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        System.Threading.Timer timer = null;
        delegate void SetTextCallback(string text);             //委托
        public int a = 0;
        private void button1_Click(object sender, EventArgs e)
        {
            //立即开始计时,时间间隔1000毫秒:
            timer.Change(0, 1000);
            //停止计时:
            //timer.Change(Timeout.Infinite, 1000);
            //暂停计时:
            //timer.Change(-1, -1);
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            timer = new System.Threading.Timer(new System.Threading.TimerCallback(ThreadMethod), null, -1, -1);  //最后两个参数依次为:多久后开始,隔多久执行一次。

        }
        public void ThreadMethod(Object state)
        {
            a++;
            SetTextCallback deg = new SetTextCallback(SetText);
            this.Invoke(deg, new object[] { a.ToString() });   //委托传值

        }

        private void SetText(string text)
        {
            label1.Text = text;
        }

    }
}

4.DispatcherTimer使用

DispatcherTimer只有wpf才能使用,和winform中的timer差不多。

记得使用using

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace WpfApp2
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        int a = 0;
        public MainWindow()
        {
            InitializeComponent();
            DispatcherTimer timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromSeconds(1);
            timer.Tick += Timer_Tick;
            timer.Start();
        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            a++;
            lblA.Content = a.ToString();
        }
    }
}

来源:C#使用计时器_c# 计时器-CSDN博客

  • 11
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

故里2130

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值