c#.net 计时器

第一步,用WPF (.NET Framework),添加个TextBox,简单设置属性。

第二步,贴代码

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; //添加Threading类

namespace ClockSample
{

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DispatcherTimer ClockOut = new DispatcherTimer(); // 类的实例化--设置对象(实例成员)
            ClockOut.Interval = TimeSpan.FromSeconds(1); //设置属性
            ClockOut.Tick += ClockOut_Tick;// 链接事件处理器
            ClockOut.Start(); 
        }

        private void ClockOut_Tick(object sender, EventArgs e)
        {
            this.ClockBox.Text = DateTime.Now.ToString();
        }
    }
}

 

心得:初识类的使用方法。 

原文地址:https://www.cnblogs.com/cnafzyx/p/11827016.html