If we need to write a program that works periodically, which way do we prefer? Writing a windows service or writing a console application which works as scheduled task?
如果我们需要编写一个定期运行的程序,我们更喜欢哪种方式?编写Windows服务或编写一个按预定任务工作的控制台应用程序?
10 个解决方案
#1
I would suggest running the process as a scheduled task if you can, and writing a service only if you need to. Services are quite a bit more difficult to write (correctly), and if you're running your process on a schedule of any sort, you're much better off using the windows scheduler than attempting to build a scheduler of your own (within the servce).
如果可以,我建议将进程作为计划任务运行,并且只在需要时编写服务。服务(正确地)编写起来要困难得多,如果你按照任何类型的计划运行你的流程,那么使用Windows调度程序比尝试构建自己的调度程序要好得多(在servce)。
- The windows task scheduler is more efficient than your home-made scheduler will be
- The windows task scheduler offers a number of options that you almost certainly won't attempt to replicate, but may find useful later on
- If you build a service, you'd have to recompile the program if you want to change the logic that determines what conditions to run the task under. If you use the scheduler, you just flip some options and are done with it.
Windows任务调度程序比您自制的调度程序更有效
Windows任务调度程序提供了许多选项,您几乎肯定不会尝试复制这些选项,但稍后可能会发现它们很有用
如果要构建服务,则必须重新编译程序,如果要更改确定运行任务的条件的逻辑。如果您使用调度程序,只需翻转一些选项即可完成。
If you're trying to decide between the two, then obviously using the Task Scheduler is a viable option. And if using the Task Scheduler is a viable option, then building a service is almost certainly the wrong choice.
如果您正在尝试在两者之间做出决定,那么显然使用任务计划程序是一个可行的选择。如果使用任务计划程序是一个可行的选择,那么构建服务几乎肯定是错误的选择。
#2
Depends on just how regular you need it to run.
取决于你需要它如何定期运行。
If its something that needs to run every 60 seconds all day, I'd go with a Windows Service.
如果它需要整天每60秒运行一次,我会使用Windows服务。
If its something that only needs to run once a day, I'd go with Scheduled Task
如果它只需要每天运行一次,我会选择计划任务
Anything in the middle... use your judgement :)
中间的任何东西......用你的判断:)
#3
Start with a console app. Seperate the logic which would sit inside your loop-process-sleep loop, then you can actually switch between them easily - even in the same EXE.
从控制台应用程序开始。分离出位于循环过程睡眠循环中的逻辑,然后您可以轻松地在它们之间切换 - 即使在相同的EXE中也是如此。
I've done this. We could call:
我做到了这一点。我们可以打电话:
ourservice.exe -console
and it'd just run. Or
而它只是运行。要么
ourservice.exe -install
and it'll install as a service :)
它将作为服务安装:)
I'd go scheduled task in 99% of cases. If you need to run all the time, listen on ports, watch a folder (maybe - can be done every 10 seconds without a problem): then do it in a service. If all you do is wake up, do some processing (or not), and then go back to sleep: use the scheduler. It's easier, cleaner (memory management, esp if you are using COM objects, and REALLY if you are using MAPI), and the options (weekly, but not on tuesdays at 5pm) with the MS scheduler are better than you can write in the time..... which is NO time, as it already exists and is free
99%的情况下,我会去预定任务。如果你需要一直运行,请监听端口,查看文件夹(可能 - 可以每10秒完成一次没有问题):然后在服务中执行。如果你所做的只是唤醒,做一些处理(或不处理),然后再回到睡眠状态:使用调度程序。它更容易,更清晰(内存管理,尤其是如果你使用COM对象,如果你使用MAPI则是真的),并且使用MS调度程序的选项(每周,但不是在星期二下午5点)比你在时间.....这是没有时间,因为它已经存在并且是免费的
Oh, and it's easier to debug a console app (scheduler) than a service.... :) Or have someone "just run it".
哦,调试控制台应用程序(调度程序)比服务更容易.... :)或者有人“只是运行它”。
#4
If it truly is "periodically" then I'd go with Scheduled Task. These can be set up to run at any desired frequency.
如果真的是“定期”,那么我会选择预定任务。这些可以设置为以任何所需的频率运行。
Services are (in my experience) more for programs that have to respond to essentially random events, such as files arriving via FTP, or ones that have to monitor the state of the file system or database.
对于必须响应基本随机事件的程序(例如通过FTP到达的文件,或者必须监视文件系统或数据库状态的程序),服务(根据我的经验)更多。
#5
Alternatively have a look at quartz.net if you want something to run periodically. It's a pre-built framework that allows you to schedule tasks and saves you writing the service part yourself.
或者,如果你想定期运行某些东西,请查看quartz.net。它是一个预构建的框架,允许您安排任务并节省您自己编写服务部分。
#6
Avoid a service unless you must use it.
除非必须使用它,否则请避免使用它。
It is a process - which means it runs all the time. Gratitious use of system resources is bad practise.
这是一个过程 - 这意味着它一直在运行。有意使用系统资源是不好的做法。
I personally find it insulting that other people have their process executing 24/7 on MY computer when it is entirely unnecessary. It's MY performance they're using up. I disable all non-necessary background services.
我个人觉得侮辱其他人在我的计算机上全天候执行他们的进程是完全没必要的。这是他们用完的表现。我禁用所有不必要的后台服务。
#7
I think a service might be useful if you want to use WCF or .NET Remoting and have client applications communicating with some host service; otherwise I agree that a scheduled task is preferable to a service if the more complicated service adds nothing new.
我认为如果您想使用WCF或.NET Remoting并让客户端应用程序与某些主机服务进行通信,那么服务可能会很有用;否则我同意,如果更复杂的服务没有增加任何新内容,则计划任务优于服务。
And @Tom, his statement about having to stay logged into a computer for a scheduled task to run is false. I just tested myself, and confirmed that a Windows schedule task will still run even if you're not logged in (unless of course you select the option that the task only runs while you're logged on).
而@Tom,他关于必须保持登录计算机以执行计划任务的声明是错误的。我刚刚测试了自己,并确认即使您没有登录,Windows计划任务仍将运行(除非您选择该任务仅在您登录时运行)。
#8
It depends on what you mean with periodically? Each second, Each minute, each hour, each day?
这取决于你定期的意思?每一秒,每一分钟,每一小时,每一天?
I would say the more frequent a task is to be running, the more a Windows Service is preferred.
我想说一个任务运行得越频繁,Windows服务就越受欢迎。
#9
"It depends", but I usually prefer scheduled tasks, for simplicity:
“这取决于”,但为了简单起见,我通常更喜欢预定的任务:
- You do not need to add any boilerplate service code to you program.
- Your program can be run from the command line for troubleshooting (you would have to use command line switches in your service program to accomplish this).
您无需向程序添加任何样板服务代码。
您的程序可以从命令行运行以进行故障排除(您必须在服务程序中使用命令行开关来完成此操作)。
If this is a more or less a one off, and something that you will install and controll yourself, a scheduled task is probably a good idea.
如果这或多或少是一次性的,并且您将自己安装和控制某些事情,那么计划任务可能是一个好主意。
For something that should have the feel of a finished product, and the customer should install themselves, I would go with a Windows Service.
对于应该具有成品感觉的东西,并且客户应该自己安装,我会使用Windows服务。
And the schedule is also an issue. The minimum schedule for a scheduled task is one minute. Anything below that, you either need to use a windows service, or build a loop into your job.
时间表也是一个问题。计划任务的最短时间表是一分钟。无论如何,你要么需要使用Windows服务,要么在你的工作中建立一个循环。
#10
I would think it would also depend on if you can leave the computer logged in or not. I've found that a Windows Scheduled Task cannot run unless the person that set up the task is logged in. If the computer cannot stay logged into, the program must be run as a service vice Scheduled Task.
我认为这还取决于你是否可以让计算机登录。我发现Windows计划任务无法运行,除非设置任务的人员已登录。如果计算机无法保持登录状态,则程序必须作为服务副计划任务运行。
#1
I would suggest running the process as a scheduled task if you can, and writing a service only if you need to. Services are quite a bit more difficult to write (correctly), and if you're running your process on a schedule of any sort, you're much better off using the windows scheduler than attempting to build a scheduler of your own (within the servce).
如果可以,我建议将进程作为计划任务运行,并且只在需要时编写服务。服务(正确地)编写起来要困难得多,如果你按照任何类型的计划运行你的流程,那么使用Windows调度程序比尝试构建自己的调度程序要好得多(在servce)。
- The windows task scheduler is more efficient than your home-made scheduler will be
- The windows task scheduler offers a number of options that you almost certainly won't attempt to replicate, but may find useful later on
- If you build a service, you'd have to recompile the program if you want to change the logic that determines what conditions to run the task under. If you use the scheduler, you just flip some options and are done with it.
Windows任务调度程序比您自制的调度程序更有效
Windows任务调度程序提供了许多选项,您几乎肯定不会尝试复制这些选项,但稍后可能会发现它们很有用
如果要构建服务,则必须重新编译程序,如果要更改确定运行任务的条件的逻辑。如果您使用调度程序,只需翻转一些选项即可完成。
If you're trying to decide between the two, then obviously using the Task Scheduler is a viable option. And if using the Task Scheduler is a viable option, then building a service is almost certainly the wrong choice.
如果您正在尝试在两者之间做出决定,那么显然使用任务计划程序是一个可行的选择。如果使用任务计划程序是一个可行的选择,那么构建服务几乎肯定是错误的选择。
#2
Depends on just how regular you need it to run.
取决于你需要它如何定期运行。
If its something that needs to run every 60 seconds all day, I'd go with a Windows Service.
如果它需要整天每60秒运行一次,我会使用Windows服务。
If its something that only needs to run once a day, I'd go with Scheduled Task
如果它只需要每天运行一次,我会选择计划任务
Anything in the middle... use your judgement :)
中间的任何东西......用你的判断:)
#3
Start with a console app. Seperate the logic which would sit inside your loop-process-sleep loop, then you can actually switch between them easily - even in the same EXE.
从控制台应用程序开始。分离出位于循环过程睡眠循环中的逻辑,然后您可以轻松地在它们之间切换 - 即使在相同的EXE中也是如此。
I've done this. We could call:
我做到了这一点。我们可以打电话:
ourservice.exe -console
and it'd just run. Or
而它只是运行。要么
ourservice.exe -install
and it'll install as a service :)
它将作为服务安装:)
I'd go scheduled task in 99% of cases. If you need to run all the time, listen on ports, watch a folder (maybe - can be done every 10 seconds without a problem): then do it in a service. If all you do is wake up, do some processing (or not), and then go back to sleep: use the scheduler. It's easier, cleaner (memory management, esp if you are using COM objects, and REALLY if you are using MAPI), and the options (weekly, but not on tuesdays at 5pm) with the MS scheduler are better than you can write in the time..... which is NO time, as it already exists and is free
99%的情况下,我会去预定任务。如果你需要一直运行,请监听端口,查看文件夹(可能 - 可以每10秒完成一次没有问题):然后在服务中执行。如果你所做的只是唤醒,做一些处理(或不处理),然后再回到睡眠状态:使用调度程序。它更容易,更清晰(内存管理,尤其是如果你使用COM对象,如果你使用MAPI则是真的),并且使用MS调度程序的选项(每周,但不是在星期二下午5点)比你在时间.....这是没有时间,因为它已经存在并且是免费的
Oh, and it's easier to debug a console app (scheduler) than a service.... :) Or have someone "just run it".
哦,调试控制台应用程序(调度程序)比服务更容易.... :)或者有人“只是运行它”。
#4
If it truly is "periodically" then I'd go with Scheduled Task. These can be set up to run at any desired frequency.
如果真的是“定期”,那么我会选择预定任务。这些可以设置为以任何所需的频率运行。
Services are (in my experience) more for programs that have to respond to essentially random events, such as files arriving via FTP, or ones that have to monitor the state of the file system or database.
对于必须响应基本随机事件的程序(例如通过FTP到达的文件,或者必须监视文件系统或数据库状态的程序),服务(根据我的经验)更多。
#5
Alternatively have a look at quartz.net if you want something to run periodically. It's a pre-built framework that allows you to schedule tasks and saves you writing the service part yourself.
或者,如果你想定期运行某些东西,请查看quartz.net。它是一个预构建的框架,允许您安排任务并节省您自己编写服务部分。
#6
Avoid a service unless you must use it.
除非必须使用它,否则请避免使用它。
It is a process - which means it runs all the time. Gratitious use of system resources is bad practise.
这是一个过程 - 这意味着它一直在运行。有意使用系统资源是不好的做法。
I personally find it insulting that other people have their process executing 24/7 on MY computer when it is entirely unnecessary. It's MY performance they're using up. I disable all non-necessary background services.
我个人觉得侮辱其他人在我的计算机上全天候执行他们的进程是完全没必要的。这是他们用完的表现。我禁用所有不必要的后台服务。
#7
I think a service might be useful if you want to use WCF or .NET Remoting and have client applications communicating with some host service; otherwise I agree that a scheduled task is preferable to a service if the more complicated service adds nothing new.
我认为如果您想使用WCF或.NET Remoting并让客户端应用程序与某些主机服务进行通信,那么服务可能会很有用;否则我同意,如果更复杂的服务没有增加任何新内容,则计划任务优于服务。
And @Tom, his statement about having to stay logged into a computer for a scheduled task to run is false. I just tested myself, and confirmed that a Windows schedule task will still run even if you're not logged in (unless of course you select the option that the task only runs while you're logged on).
而@Tom,他关于必须保持登录计算机以执行计划任务的声明是错误的。我刚刚测试了自己,并确认即使您没有登录,Windows计划任务仍将运行(除非您选择该任务仅在您登录时运行)。
#8
It depends on what you mean with periodically? Each second, Each minute, each hour, each day?
这取决于你定期的意思?每一秒,每一分钟,每一小时,每一天?
I would say the more frequent a task is to be running, the more a Windows Service is preferred.
我想说一个任务运行得越频繁,Windows服务就越受欢迎。
#9
"It depends", but I usually prefer scheduled tasks, for simplicity:
“这取决于”,但为了简单起见,我通常更喜欢预定的任务:
- You do not need to add any boilerplate service code to you program.
- Your program can be run from the command line for troubleshooting (you would have to use command line switches in your service program to accomplish this).
您无需向程序添加任何样板服务代码。
您的程序可以从命令行运行以进行故障排除(您必须在服务程序中使用命令行开关来完成此操作)。
If this is a more or less a one off, and something that you will install and controll yourself, a scheduled task is probably a good idea.
如果这或多或少是一次性的,并且您将自己安装和控制某些事情,那么计划任务可能是一个好主意。
For something that should have the feel of a finished product, and the customer should install themselves, I would go with a Windows Service.
对于应该具有成品感觉的东西,并且客户应该自己安装,我会使用Windows服务。
And the schedule is also an issue. The minimum schedule for a scheduled task is one minute. Anything below that, you either need to use a windows service, or build a loop into your job.
时间表也是一个问题。计划任务的最短时间表是一分钟。无论如何,你要么需要使用Windows服务,要么在你的工作中建立一个循环。
#10
I would think it would also depend on if you can leave the computer logged in or not. I've found that a Windows Scheduled Task cannot run unless the person that set up the task is logged in. If the computer cannot stay logged into, the program must be run as a service vice Scheduled Task.
我认为这还取决于你是否可以让计算机登录。我发现Windows计划任务无法运行,除非设置任务的人员已登录。如果计算机无法保持登录状态,则程序必须作为服务副计划任务运行。