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条回答
beautiful°
2楼-- · 2019-01-18 00:10

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

查看更多
你好瞎i
3楼-- · 2019-01-18 00:11

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

查看更多
手持菜刀,她持情操
4楼-- · 2019-01-18 00:12

System.Threading.Timer might work out for you.

Documented here

查看更多
Rolldiameter
5楼-- · 2019-01-18 00:13

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.

查看更多
劳资没心,怎么记你
6楼-- · 2019-01-18 00:16

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

查看更多
时光不老,我们不散
7楼-- · 2019-01-18 00:17

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.

查看更多
登录 后发表回答