• 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一个

    代码:

    1. using System;
    2. using System.Collections.Generic;
    3. using System.ComponentModel;
    4. using System.Data;
    5. using System.Drawing;
    6. using System.Linq;
    7. using System.Text;
    8. using System.Threading.Tasks;
    9. using System.Windows.Forms;
    10. namespace WindowsFormsApp1
    11. {
    12. public partial class Form1 : Form
    13. {
    14. public Form1()
    15. {
    16. InitializeComponent();
    17. }
    18. Timer timer = new Timer();
    19. public int a = 0;
    20. private void button1_Click(object sender, EventArgs e)
    21. {
    22. timer.Start();
    23. timer.Tick += Timer_Tick;
    24. }
    25. private void Timer_Tick(object sender, EventArgs e)
    26. {
    27. a++;
    28. label1.Text = a.ToString();
    29. }
    30. private void Form1_Load(object sender, EventArgs e)
    31. {
    32. timer.Enabled = true;
    33. timer.Interval = 1000;
    34. }
    35. }
    36. }

    2.System.Timers.Timer使用

    2种方式

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

    代码

    1. using System;
    2. using System.Collections.Generic;
    3. using System.ComponentModel;
    4. using System.Data;
    5. using System.Drawing;
    6. using System.Linq;
    7. using System.Text;
    8. using System.Threading.Tasks;
    9. using System.Windows.Forms;
    10. namespace WindowsFormsApp1
    11. {
    12. public partial class Form1 : Form
    13. {
    14. public Form1()
    15. {
    16. InitializeComponent();
    17. }
    18. System.Timers.Timer timer = new System.Timers.Timer();
    19. public int a = 0;
    20. private void button1_Click(object sender, EventArgs e)
    21. {
    22. timer.Start();
    23. timer.Elapsed += Timer_Elapsed;
    24. }
    25. private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    26. {
    27. a++;
    28. label1.Text = a.ToString();
    29. }
    30. private void Form1_Load(object sender, EventArgs e)
    31. {
    32. timer.Enabled = true;
    33. timer.Interval = 1000;
    34. timer.SynchronizingObject = this;
    35. }
    36. }
    37. }

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

    代码

    1. using System;
    2. using System.Collections.Generic;
    3. using System.ComponentModel;
    4. using System.Data;
    5. using System.Drawing;
    6. using System.Linq;
    7. using System.Text;
    8. using System.Threading.Tasks;
    9. using System.Windows.Forms;
    10. namespace WindowsFormsApp1
    11. {
    12. public partial class Form1 : Form
    13. {
    14. public Form1()
    15. {
    16. InitializeComponent();
    17. }
    18. System.Timers.Timer timer = new System.Timers.Timer();
    19. delegate void SetTextCallback(string text); //委托
    20. public int a = 0;
    21. private void button1_Click(object sender, EventArgs e)
    22. {
    23. timer.Start();
    24. timer.Elapsed += Timer_Elapsed;
    25. }
    26. private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    27. {
    28. a++;
    29. SetTextCallback deg = new SetTextCallback(SetText);
    30. this.Invoke(deg, new object[] { a.ToString() }); //委托传值
    31. }
    32. private void Form1_Load(object sender, EventArgs e)
    33. {
    34. timer.Enabled = true;
    35. timer.Interval = 1000;
    36. }
    37. private void SetText(string text)
    38. {
    39. label1.Text = text;
    40. }
    41. }
    42. }

    3.System.Threading.Timer使用

    代码

    1. using System;
    2. using System.Collections.Generic;
    3. using System.ComponentModel;
    4. using System.Data;
    5. using System.Drawing;
    6. using System.Linq;
    7. using System.Text;
    8. using System.Threading;
    9. using System.Threading.Tasks;
    10. using System.Windows.Forms;
    11. namespace WindowsFormsApp1
    12. {
    13. public partial class Form1 : Form
    14. {
    15. public Form1()
    16. {
    17. InitializeComponent();
    18. }
    19. System.Threading.Timer timer = null;
    20. delegate void SetTextCallback(string text); //委托
    21. public int a = 0;
    22. private void button1_Click(object sender, EventArgs e)
    23. {
    24. //立即开始计时,时间间隔1000毫秒:
    25. timer.Change(0, 1000);
    26. //停止计时:
    27. //timer.Change(Timeout.Infinite, 1000);
    28. //暂停计时:
    29. //timer.Change(-1, -1);
    30. }
    31. private void Form1_Load(object sender, EventArgs e)
    32. {
    33. timer = new System.Threading.Timer(new System.Threading.TimerCallback(ThreadMethod), null, -1, -1); //最后两个参数依次为:多久后开始,隔多久执行一次。
    34. }
    35. public void ThreadMethod(Object state)
    36. {
    37. a++;
    38. SetTextCallback deg = new SetTextCallback(SetText);
    39. this.Invoke(deg, new object[] { a.ToString() }); //委托传值
    40. }
    41. private void SetText(string text)
    42. {
    43. label1.Text = text;
    44. }
    45. }
    46. }

    4.DispatcherTimer使用

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

    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. using System.Threading.Tasks;
    6. using System.Windows;
    7. using System.Windows.Controls;
    8. using System.Windows.Data;
    9. using System.Windows.Documents;
    10. using System.Windows.Input;
    11. using System.Windows.Media;
    12. using System.Windows.Media.Imaging;
    13. using System.Windows.Navigation;
    14. using System.Windows.Shapes;
    15. using System.Windows.Threading;
    16. namespace WpfApp2
    17. {
    18. ///
    19. /// MainWindow.xaml 的交互逻辑
    20. ///
    21. public partial class MainWindow : Window
    22. {
    23. int a = 0;
    24. public MainWindow()
    25. {
    26. InitializeComponent();
    27. DispatcherTimer timer = new DispatcherTimer();
    28. timer.Interval = TimeSpan.FromSeconds(1);
    29. timer.Tick += Timer_Tick;
    30. timer.Start();
    31. }
    32. private void Timer_Tick(object sender, EventArgs e)
    33. {
    34. a++;
    35. lblA.Content = a.ToString();
    36. }
    37. }
    38. }

  • 相关阅读:
    Java ~ Reference ~ FinalReference/Finalizer
    JAVA实现冒泡排序
    计算机图像处理:椒盐噪声和高斯噪声
    说说用户线程和守护线程
    基础复习——图像视图ImageView——图像按钮ImageButton——同时展示文本与图像...
    软件设计师_数据库——关系代数
    lambda表达式(第二十二天)
    Open suse 15.4 Leep 服务环境部署过程及避坑
    OpenCV之直方图均衡化-----(对比度的方法之一)
    1.7.3、计算机网络体系结构分层思想举例
  • 原文地址:https://blog.csdn.net/u012563853/article/details/126231513