Home  >  Article  >  Web Front-end  >  Example code sharing of c# timer

Example code sharing of c# timer

零下一度
零下一度Original
2017-06-26 14:49:301349browse
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 计时器
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            label1.Text = DateTime.Now.ToString();
            System.Timers.Timer t = new System.Timers.Timer(1000);//实例化Timer类,设置间隔时间为1000毫秒 就是1秒;
            t.Elapsed += new System.Timers.ElapsedEventHandler(theout);//到达时间的时候执行事件;
            t.AutoReset = true;//设置是执行一次(false)还是一直执行(true);
            t.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件;
           
        }
        public void theout(object source, System.Timers.ElapsedEventArgs e)
        {
            this.Invoke(new TextOption(f));//invok 委托实现跨线程的调用
        }

        delegate void TextOption();//定义一个委托

        void f()
        {
            label1.Text = DateTime.Now.ToString();//调用内容,并用lable1显示出来。。。
        }

       
    }
}

The above is the detailed content of Example code sharing of c# timer. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:datatables usage learningNext article:datatables usage learning