views:

1298

answers:

8

So here's my problem, I need to do a c# service running on a server who's getting file on ftp one time per day at 3am. I think that I can do it with a thread.sleep() or by compare the DateTime.Now with 3am....

Do you have better solution?

Thank you very much for your help!

+3  A: 

Write a console app or equivalent, and use the Windows Scheduler (or whatever it's called nowadays...) to run it daily.

Tomas Lycken
+1  A: 

Hi,

System.Threading.Timer might work out for you.

Documented here

KB22
+1  A: 

Why not set this up to run as a scheduled task. Have it execute and then exit. Set up Windows to start the process as a scheduled task at 3:00 AM daily.

Michael Meadows
+3  A: 

For a flexible solution, you could use this. It's not a Windows service, but you could easily incorporate it into one. Or you might consider Quartz.NET which is reputed to be industrial strength (the Java version certainly is).

Vinay Sajip
+1 for the Quartz.NET reference - this would be the correct and indeed robust choice to address scheduling needs within a custom solution (and should be preferred hands down over the limited custom cron you mentioned, using Quartz is remarkably easy), though for the task at hand just using the Task Scheduler as per the accepted answer is definitely the way to go.
Steffen Opel
+32  A: 

Much better solution, let windows do it for you using Scheduled Tasks.

If it only needs to be executed once a day, then I would go for this. Even Google does it ;)

EDIT:

To answer the comment, it is as Fredrik has said. It is an Advanced Option, which you can open as the task is set up, containing a checkbox called: "Run only if logged on", which is false to begin with.

Kyle Rozendo
I recomend! For my company, currently it's the best solution.
Zanoni
This is definitely the way to go; then your app will not use any memory while it's only waiting to run tomorrow.
Fredrik Mörk
Change the C# Service to a Windows application no forms.
Zanoni
Services are applications that run **all the time**. Scheduled tasks are applications that run **at specific times**. +1.
Will
if there's no session open on the server does the scheduled task will run ?
Bigballs
@Bigballs: that is configurable; when creating the task you can select whether it should be run only if a user is logged on, or if it should run regardless of that.
Fredrik Mörk
+2  A: 

Other answers are good. I just thought I'd point out that

compare the DateTime.Now with 3am

is a bad solution, even if you sleep for some time between each check, as it wastes system resources unnecessarily (not only in repeatedly checking the time, but also in the memory usage of the program).

Artelius
+2  A: 

I've used Scheduled Tasks successfully for backups, but I have a word of caution ...

Scheduled tasks are not be performed if you log out. I'm not sure if this can be overidden, but I have been caught out by tasks not performed because Windows automatically updates, reboots and sits waiting for me to log-in.

I disabled automatic updates - Windows should ASK first.

Another consideration is that 3AM is a time when many users would normally be logged out.

pavium
This is what I'm thinking...I've try the scheduled task and after the server reboot nothing start... Thank you!
Bigballs
I must say, I have never had the issue myself. Always worked 100% for me.
Kyle Rozendo
+1  A: 

If you are expecting a file every night, instead of worrying about when it arrives, just get an event fired when it does.

Look at:

System.IO.FileSystemWatcher

File System Watcher on MSDN

omantn
+1 Nice class, thanks :)
Kyle Rozendo