安排 C# 控制台应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18739396/
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
Schedule a C# console application
提问by SuicideSheep
I've given a task to create an email C# console application which targeted to run as batch. I'm very new to C# area and hence I have no idea about my direction. This C# console application will be deployed on a server and expect to run in certain time based on server time.
我给了一个任务来创建一个电子邮件 C# 控制台应用程序,该应用程序旨在作为批处理运行。我对 C# 领域很陌生,因此我不知道我的方向。此 C# 控制台应用程序将部署在服务器上,并期望根据服务器时间在特定时间运行。
After some research, most of the suggestions are about using task scheduler or window services, but I'm wondering if this C# console application is somehow possible to run own its own? Maybe after execute it, it then register itself into the server and the server will handle it periodically?
经过一些研究,大多数建议都是关于使用任务调度程序或窗口服务的,但我想知道这个 C# 控制台应用程序是否可以自己运行?也许执行后,它然后将自己注册到服务器中,服务器会定期处理它?
采纳答案by sam
If you want to manage the scheduler by code have a look at Quartz.NET: http://quartznet.sourceforge.net/
如果你想管理由代码调度看看Quartz.NET:http://quartznet.sourceforge.net/
This library provides the creation of jobs and triggers.
该库提供作业和触发器的创建。
e.g. (directly from: http://quartznet.sourceforge.net/tutorial/lesson_3.html):
例如(直接来自:http: //quartznet.sourceforge.net/tutorial/lesson_3.html):
// construct a scheduler factory
ISchedulerFactory schedFact = new StdSchedulerFactory();
// get a scheduler
IScheduler sched = schedFact.GetScheduler();
sched.Start();
// construct job info
JobDetail jobDetail = new JobDetail("myJob", null, typeof(DumbJob));
// fire every hour
Trigger trigger = TriggerUtils.MakeHourlyTrigger();
// start on the next even hour
trigger.StartTime = TriggerUtils.GetEvenHourDate(DateTime.UtcNow);
trigger.Name = "myTrigger";
sched.ScheduleJob(jobDetail, trigger);
Now this means your console application should run continuously which makes it not really suitable for the job.
现在这意味着您的控制台应用程序应该连续运行,这使得它不太适合这项工作。
Option 1. Add a task via task scheduler (real easy) that executes your console application. See example task: http://www.sevenforums.com/tutorials/12444-task-scheduler-create-new-task.html
选项 1. 通过执行控制台应用程序的任务调度程序(非常简单)添加任务。请参阅示例任务:http: //www.sevenforums.com/tutorials/12444-task-scheduler-create-new-task.html
Option 2. Create a windows service (not to complicated) that uses a library like Quartz.NET or .NET's Timerclass to scheduele jobs and executes a batch operation. See for creation of windows service http://msdn.microsoft.com/en-us/library/zt39148a.aspx
选项 2. 创建一个 Windows 服务(不要太复杂),它使用 Quartz.NET 或 .NET 的Timer类等库来调度作业并执行批处理操作。有关创建 Windows 服务的信息,请参见http://msdn.microsoft.com/en-us/library/zt39148a.aspx
Option 3. Make your console application implement a scheduele library like Quartz.NET or /Net's Timerclass and run it as a service (a bit more complex): Create Windows service from executable
选项 3. 使您的控制台应用程序实现像 Quartz.NET 或 /Net 的Timer类这样的调度程序库并将其作为服务运行(有点复杂):从可执行文件创建 Windows 服务
回答by Gerrie Schenck
As long as it requires no user input, it will be able to run on it's own. You can schedule the program to run in Windows Task Scheduler for example.
只要它不需要用户输入,它就可以自行运行。例如,您可以安排程序在 Windows 任务计划程序中运行。