如何安排 C# Windows 服务每天运行一个方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/922444/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 03:17:02  来源:igfitidea点击:

How to schedule a C# Windows Service to run a method daily?

c#windowswindows-servicesscheduling

提问by Victor Rodrigues

Possible Duplicate:
How might I schedule a C# Windows Service to perform a task daily?

可能的重复:
我如何安排 C# Windows 服务每天执行任务?

I am creating a C# Windows Service, but I didn't figure out the best way to make timer fire a method daily at a specific time specified in App.Config file (e.g. daily at 6:00AM, my method is executed).

我正在创建一个 C# Windows 服务,但我没有找到让计时器每天在 App.Config 文件中指定的特定时间触发一个方法的最佳方法(例如每天早上 6:00,我的方法被执行)。

How do you do it?

你怎么做呢?

Thanks

谢谢

回答by aleemb

This code should do it:

这段代码应该这样做:

Trigger tg = new DailyTrigger();
ScheduledTasks st = new ScheduledTasks();
Task t = st.OpenTask("foo");
t.Triggers.Add(tg);
t.Save();

回答by ChrisF

Create a scheduled task. It's by far the easiest way. If you've got enough access to install a service you should have enough access to set up a scheduled task.

创建计划任务。这是迄今为止最简单的方法。如果您有足够的权限来安装服务,那么您应该有足够的权限来设置计划任务。