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.
Have a Bing for Timer instead... That ought get you there... One decent one in particular: 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");
}
}
精彩评论