c# windows 服务还是普通程序?

发布于 2024-10-27 19:10:03 字数 660 浏览 4 评论 0原文

可能的重复:
Windows 服务与计划任务
Windows 服务与简单程序

我正在尝试创建一个定期运行的程序(例如每 5 分钟),或者运行程序并让它执行其中的函数(每 5 分钟)。

该程序在执行时从数据库获取数据,然后将其写入(目前)例如 info.txt 文件(此处不包含敏感内容)。每次写入文件时,它都应该覆盖文件中的现有信息。

该程序也应该在 Windows 启动时自动启动。 (因此无需登录计算机并执行 .exe [如果它是普通程序而不是服务])

在执行程序的期间之间无需执行任何操作。

因此,我应该将此程序作为 Windows 服务运行,还是应该使用任务计划程序定期启动该程序来执行此操作? 我的目标是让这个程序尽可能顺利地运行而不堵塞资源。 (例如,它不需要超过 5% 的 cpu)

我希望我的问题足够清楚。

Possible Duplicates:
windows service vs scheduled task
Windows Service Vs Simple Program

I'm trying to create a program that runs periodically (say every 5 minutes), or have the program running and have it execute the function within it (every 5 minutes).

The program is to obtain data from a database when it's executed and then write this to (for now) say info.txt file (no sensitive stuff is contained in here). each time it writes to the file it should overwrite the existing info within the file.

The program should also be started automatically at windows start up. (thus no need to login on the machine and to execute the .exe [if it's a normal program and not a service])

In between the periods that it executes the program would have nothing to do.

Therefore, should I run this program as a Windows Service, or should I use the Task Scheduler to periodically start the program to do this?
My goal is for this program to run as smooth as possible without clogging up resources. (eg. it shouldn't need more than 5% of the cpu)

I hope my question was clear enough.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你不是我要的菜∠ 2024-11-03 19:10:03

我会选择由任务调度程序触发的应用程序。您唯一需要担心的是运行应用程序的单个实例。

您可以将任务设置为在特定用户帐户下运行,并且即使用户未登录也可以运行。有许多事件可以触发任务星,例如“Windows 启动”、“系统空闲”...

其他优点是:如果出现崩溃,您可以设置任务计划程序向您发送电子邮件或以多种方式提醒您。您可以控制应用程序的“退出代码”,并向任务调度程序发出信号,指示正在发生什么以及要做什么。

任务计划程序提供了很多积极的功能,但使用它们的人并不多。

I would go with application that is triggered by task scheduler. Only thing that you need to worry about is to run single instance of your application.

You can set task to run under specific user account and to run even if user is not logged on. There are number of events that can trigger task star like "Windows start", "System Idle"...

Other advantage is: If something crashes you can set task scheduler to send you an email or alert you in number of ways. You can control "exit code" of your application and signal to task scheduler what's going on and what to do.

There are a lot of positive features that task scheduler offers but not many people are using them.

二智少女 2024-11-03 19:10:03

我建议为此使用 Windows 服务。创建两者并比较资源使用情况可能是个好主意?

I would suggest a Windows Service for this. Might be a good idea to create both and compare what the resource usage is like anyway?

无妨# 2024-11-03 19:10:03

实际上,我建议根据您希望运行的任务的要求来选择两者。我通常将计划服务的大部分功能构建到单个类库中,然后将其包装在控制台应用程序中以启动和调试。当满意时,我将其包装在 Windows 服务中并忘记它。

使用控制台应用程序的注意事项:

  • 如果可能,请确保在系统帐户下运行它,或者您可以在“按调度程序中的方式运行”下输入特定的登录名。这将确保不需要交互式登录。
  • 如果同时运行它的 2 个实例是一个问题,请确保您清楚地命名它,并检查它的实例是否在主方法中运行,如果是,则退出。 Windows 服务将避免这个问题。

使用窗口服务的注意事项

  • 确保您了解线程的使用。如果管理得当,Windows 服务将使用更少的资源,但如果它对您来说是新的,并且最终会在基于计时器的任务中泄漏内存,则可能会很棘手。

..还有很多事情需要考虑,但是正确编码,您可以从第一个开始,当您对此有信心时转向第二个。

I would actually recommend going for both depending on the requirements of the task you wish to run. I generally build most functionality for scheduled services into a single class library and then wrap it in a console application to start with and for debugging. When satisfied I wrap it in a windows service and forget about it.

Considerations of using a console app:

  • Make sure you run it under system account if possible or you can put in a specific login under Run as in scheduler. This will ensure an interactive login is not required.
  • If having 2 instances of it running at the same time is a problem, make sure you name it clearly and check for an instance of it running in your main method and exit if it is. A windows service will avoid this issue.

Considerations of using a window service

  • Make sure you're educated on thread usage. Windows services will use less resources if managed properly, but can be tricky if it's new to you and end up leaking memory in timer based tasks.

..there's a lot more to consider, but code it right and you can start with one and move to the 2nd when you're confident about it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文