简体   繁体   中英

Attach an event handler to a StopWatch

I would like to attach an event handler to a Stop Watch. Can someone please provide a code snippet in C# or VB?

I'v Bing'd it with no luck.

有一个Bing 定时器而不是...那应该让你到那里......特别是一个体面的: http//dotnetperls.com/timer

here is an example of a Timer I use for a backup program I wrote that will start in 5 seconds after instantiating it and fire the callback (tick) every hour:

    private System.Threading.Timer AutoRunTimer; 

    private void Form1_Load(object sender, EventArgs e)
    {
        mybackups = new BackupService();
        AutoRunTimer = new System.Threading.Timer(tick, "", new TimeSpan(0, 0, 5), new TimeSpan(1, 0, 0));     
    }

    private void tick(object data)
    {
        if (DateTime.Now.Hour == 1 && DateTime.Now.Subtract(lastRun).Hours >= 1) // run backups at first opportunity after 1 am
        {
            startBackup("automatically");
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM