当前位置:网站首页>C# using timer
C# using timer
2022-08-11 00:33:00 【Hometown 2130】
目录
c#The timer is4种:
- Timer timer = new Timer(),控件
- System.Timers.Timer timer2 = new System.Timers.Timer();代码
- System.Threading.Timer threadTimer = new System.Threading.Timer( ); 代码
- DispatcherTimer dispatcherTimer = new DispatcherTimer();代码
winform中可以使用的是:123
WPF中可以使用的是:234
其中23Don't rely on the form
1.Timer使用
可以在winformThe toolbar directly drag a control

Can also be in the code yourselfnew一个
代码:
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 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 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 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); //The last two parameters in the order:多久后开始,How often do a.
}
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 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();
}
}
}
边栏推荐
猜你喜欢

YOLOv5的Tricks | 【Trick10】从PyTorch Hub加载YOLOv5

Is there a way out in the testing industry if it is purely business testing?

百战RHCE(第四十八战:运维工程师必会技-Ansible学习3-构建Ansible清单)

20张图,全面掌握MVCC原理!

只会懒汉式和饿汉式 你还不懂单例模式!
![[Excel knowledge and skills] Convert](/img/96/ece9c3885fd4abe4bf4d211813b9c4.png)
[Excel knowledge and skills] Convert "false" date to "true" date format

盘点美军的无人机家底

容器技术真的是环境管理的救星吗?

构建检测,无规矩不成方圆

力扣------使用最小花费爬楼梯
随机推荐
从0开始设计JVM ,忘记名词跟上思路一次搞懂
10. Notes on receiving parameters
rhel7.0解决yum无法使用(system is not registered to Red Hat Subscription Management)
软件测试证书(1)—— 软件评测师
YOLOv5的Tricks | 【Trick12】YOLOv5使用的数据增强方法汇总
C# JObject解析JSON数据
Pico 4更多参数曝光:Pancake+彩色透视,还有Pro版本
Jvm. Profiling tools (jconsole, jvisualvm, arthas, jprofiler, mat)
Software Testing Certificate (1) - Software Evaluator
How to check if the online query suddenly slows down
云原生-VMware虚拟机安装Kubesphere实战(一)
Mysql数据库安装配置详细教程
How to do patent mining, the key is to find patent points, in fact, it is not too difficult
[21-day learning challenge - kernel notes] (5) - devmem read and write register debugging
[Excel knowledge and skills] Convert text numbers to numeric format
Server Tips
[数据可视化] 图表设计原则
线上突然查询变慢怎么核查
Lens filter---about day and night dual-pass filter
Special class and type conversion