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中的工具栏中直接拖一个控件

C#使用计时器_第1张图片

也可以在代码中自己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
{
    /// 
    /// MainWindow.xaml 的交互逻辑
    /// 
    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#,开发语言)