1.不依赖ui 的方案
1.1 Threading.Timer
1.1.1代码
using System;
using System.Threading;
namespace _4种定时器
{
class Program
{
int index = 0;
static void Main(string[] args)
{
Console.WriteLine("_4种定时器-System.Threading.Timer");
Program p = new Program();
p.main();
Console.ReadKey();
}
private void main()
{
//5000毫秒后执行,间隔200毫秒
System.Threading.Timer timer = new System.Threading.Timer(new TimerCallback(timerCall), this, 1000, 200);//创建定时器
}
private void timerCall(object obj)
{
Console.WriteLine(index++);
//timer.Dispose();//释放定时器
}
}
}
1.1.2 运行效果
1.2 System.Timers.Timer
1.2.1 代码
using System;
using System.Threading;
namespace _4种定时器
{
class Program
{
int index = 0;
static void Main1(string[] args)
{
Console.WriteLine("_4种定时器-System.Threading.Timer");
Program p = new Program();
p.main();
Console.ReadKey();
}
private void main()
{
//5000毫秒后执行,间隔200毫秒
System.Threading.Timer timer = new System.Threading.Timer(new TimerCallback(timerCall), this, 1000, 200);//创建定时器
}
private void timerCall(object obj)
{
Console.WriteLine(index++);
//timer.Dispose();//释放定时器
}
}
}
1.2.2 运行效果
2.依赖ui 线程的方法
2.1 System.Windows.Forms.Timer
2.1.1 代码
using System;
using System.Windows.Forms;
namespace _4种定时器
{
public class System_Windows_Forms_Timer
{
static void Main(string[] args)
{
Console.WriteLine("_4种定时器 System_Windows_Forms_Timer");
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MyForm());
Console.ReadKey();
}
}
public class MyForm : Form
{
private System.ComponentModel.IContainer components = null;
private int index = 0;
System.Windows.Forms.Timer timer;//创建定时器
public MyForm()
{
main();
}
private void main()
{
this.components = new System.ComponentModel.Container();
timer = new System.Windows.Forms.Timer(components);//创建定时器
timer.Tick += new EventHandler(timer1_Tick);//事件处理
//timer.Enabled = true;//设置启用定时器
//timer.Interval = 500;//执行时间
timer.Start();//开启定时器
}
private void timer1_Tick(object sender, EventArgs e)
{
Console.WriteLine(index++);
//timer.Stop();//停止定时器
// timer.Tick -= new EventHandler(timer1_Tick);//取消事件
// timer.Enabled = false;//设置禁用定时器
}
}
}
2.1.2 运行效果
2.2 System.Windows.Threading.DispatcherTimer
2.2.1 代码
2.2.1.1 程序代码
using System;
using System.Windows;
namespace 窗体定时器WPF
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
int index = 0;
private static System.Windows.Threading.DispatcherTimer timer = new System.Windows.Threading.DispatcherTimer();
public MainWindow()
{
InitializeComponent();
initTime();
}
private void initTime()
{
timer.Tick += new EventHandler(timer_out);//执行事件
timer.Interval = new TimeSpan(0, 0, 0, 1);//1s执行
timer.IsEnabled = true;//启用
timer.Start();//开启
}
public void timer_out(object sender, EventArgs e)
{
textBox1.Text = (index++).ToString();
//timer.Tick -= new EventHandler(timer_out);//取消执行事件;
//timer.IsEnabled = false;//禁用
//timer.Stop();//停止
}
}
}
2.2.1.2 view 代码
<Window x:Class="窗体定时器WPF.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:窗体定时器WPF"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<TextBox x:Name="textBox1" HorizontalAlignment="Left" Height="23" Margin="390,185,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
</Grid>
</Window>
2.2.2 运行效果