tags:

views:

1212

answers:

4

Hello everyone.

I made a little Windows Forms program to do some auto backup of some files on my disk. Right now I need to push a button before it is executed, but I want to make the program running and at 3am run the function that does the backup.

I've researched a bit and found out i need a timer for this, but I've failed to use it so it executes at a specific time.

I hope you can help me out. :-)

Thanks!

+1  A: 

System.Forms.Timer

If you dont know the amount of time untill you want to Execute do something like

DateTime TimeToExecuteTask DateTime Now // assign values.

int SecondsToExectution = (TimeSpan)(TimeToExecuteTask - Now).TotalSeconds;

EKS
+1  A: 

If you want to use a timer then set it to fire every minute for example (myTimer.Interval = 60000;), in the delegate function that's called, compare the current time with your target time and if they match, launch your backup process.

Lazarus
+1  A: 

You can use the task scheduler in Windows to set programs to run at a specific time. Check out this link. It refers to the built-in Windows backup but there's no reason you can't have it run your own program instead.

Actually, this link to the AT command might be better.

I know you were looking for a programming solution (and several others have posted on how to use timers) but the advantage of scheduling something with the AT command is that your program does not need to remain running on the system all of the time. The task scheduler will start your program at the right time, it can execute and then it exits. You might need to tweak your program a little to have it backup automatically and then exit but that should be possible.

TLiebe
This is my preferred solution when it comes to tasks that don't require the program running all the time.
Thorsten Dittmar
+2  A: 

Not a 100% dupe, but you'll find this SO question interesting. ;)

Contains references to quartzTimer and other options.

KB22