当前位置:网站首页>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();
}
}
}
边栏推荐
猜你喜欢
随机推荐
sqlmap combined with dnslog fast injection
web 性能提升(将持续更新……)
rhel7.0解决yum无法使用(system is not registered to Red Hat Subscription Management)
electron -autoUpdater 更新
使用mysql语句操作数据表(table)
HW-蓝队工作流程(1)
② 关系数据库标准语言SQL 数据定义(创建、修改基本表)、数据更新(增删改)
Analysis of LENS CRA and SENSOR CRA Matching Problems
Pagoda Test-Building PHP Online Mock Exam System
[21-day learning challenge - kernel notes] (5) - devmem read and write register debugging
leetcode 前K个高频单词
NOR FLASH闪存芯片ID应用之软件保护场景
Design and implementation of flower online sales management system
关于科研学习中的几个问题:如何看论文?如何评价工作?如何找idea?
力扣------使用最小花费爬楼梯
C# JObject解析JSON数据
HW-常见攻击方式和漏洞原理(2)
地下管廊可视化管理系统搭建
CF1427F-Boring Card Game【贪心】
2022.8.10-----leetcode.640

![[Excel知识技能] 将数值格式数字转换为文本格式](/img/fb/79d6928456f090d47f0fe7a5074979.png)


![[Excel knowledge and skills] Convert text numbers to numeric format](/img/7e/16a068025ec2639b343436c7f5b245.png)




