Attach an event handler to a StopWatch

孤街醉人 提交于 2019-12-08 04:51:31

问题


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.


回答1:


Have a Bing for Timer instead... That ought get you there... One decent one in particular: http://dotnetperls.com/timer




回答2:


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");
        }
    }


来源:https://stackoverflow.com/questions/2862177/attach-an-event-handler-to-a-stopwatch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!