当前位置:网站首页>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();
}
}
}
边栏推荐
- 百战RHCE(第四十八战:运维工程师必会技-Ansible学习3-构建Ansible清单)
- 李彦宏拆墙交朋友,大厂“塑料友情”能否帮百度啃下硬骨头?
- SQL injection base
- Special class and type conversion
- 【openpyxl】过滤和排序
- Navicat 16-数据库工具
- In 22 years, the salary of programmers nationwide in January was released, only to know that there are so many with annual salary of more than 400,000?
- 5. Lombok
- 小程序onPageNotFound的坑
- ③ 关系数据库标准语言SQL 数据查询(SELECT)
猜你喜欢
"NIO Cup" 2022 Nioke Summer Multi-School Training Camp 2 DGHJKL Problem Solution
Special class and type conversion
构建资源的弹性伸缩
地下管廊可视化管理系统搭建
[Excel knowledge and skills] Convert text numbers to numeric format
使用 BeanUtils 做属性拷贝,性能有点拉胯!
线上突然查询变慢怎么核查
YOLOv5的Tricks | 【Trick11】在线模型训练可视化工具wandb(Weights & Biases)
“蔚来杯“2022牛客暑期多校训练营2 DGHJKL题解
C#使用计时器
随机推荐
地下管廊可视化管理系统搭建
Mysql数据库安装配置详细教程
什么是“门”电路(电子硬件)
深度解析volatile关键字(保证够全面)
两个数组的交集
CF1534F2-Falling Sand (Hard Version)
单片机人机交互--矩阵按键
如何利用原生JS实现回到顶部以及吸顶效果
PMP每日一练 | 考试不迷路-8.10(包含敏捷+多选)
C# JObject解析JSON数据
【pypdf2】安装、读取和保存、访问页面、获取文本、读写元数据、加密解密
LENS CRA和SENSOR CRA匹配问题解析
小程序onPageNotFound的坑
详解JDBC的实现与优化(万字详解)
C#使用计时器
2. Dependency management and automatic configuration
NOR FLASH闪存芯片ID应用之软件保护场景
Jvm.分析工具(jconsole,jvisualvm,arthas,jprofiler,mat)
从0开始设计JVM ,忘记名词跟上思路一次搞懂
91.(cesium之家)cesium火箭发射模拟