How to execute code in c# service one time per day

2019-01-17 23:41发布

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!

8条回答
叼着烟拽天下
2楼-- · 2019-01-18 00:23

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).

查看更多
男人必须洒脱
3楼-- · 2019-01-18 00:28

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.

查看更多
登录 后发表回答