如何开发Windows计划任务?

时间:2022-03-21 05:23:15

I need to have a process that compiles daily data into an PDF that will be attached into an email. I have already used MSSQL to send emails in the application being developed and would like to continue to use MSSQL as the email processor.

我需要一个将每日数据编译成PDF的进程,该PDF将附加到电子邮件中。我已经使用MSSQL在正在开发的应用程序中发送电子邮件,并希望继续使用MSSQL作为电子邮件处理器。

In my research, I have found that this can be achieved by creating a Windows Service. But, since I am only really wanting this process to run once a day; keeping an always active Windows Service seems to be overkill.

在我的研究中,我发现这可以通过创建Windows服务来实现。但是,因为我真的只想让这个过程每天运行一次;保持一个永远活跃的Windows服务似乎有点矫枉过正。

I have never used task scheduler to run a self-built application. I want to make sure that I am going about it the correct way. As I understand, I will have to...

我从未使用任务调度程序来运行自构建的应用程序。我想确保以正确的方式进行。据我了解,我将不得不......

  1. Create a new project (Project X) in Visual Studio
  2. 在Visual Studio中创建一个新项目(Project X)

  3. Program (Project X) to generate the PDF and interact with MSSQL to send the email
  4. 程序(Project X)生成PDF并与MSSQL交互以发送电子邮件

  5. Build (Project X) into an EXE
  6. 将(项目X)构建到EXE中

  7. Schedule the EXE using Task Scheduler to run on whatever schedule I need.
  8. 使用任务计划程序安排EXE以我需要的任何计划运行。

Are there any security aspects I should be concerned about? Only ones I can think of are encrypting my MSSQL connection string; and, of course, keeping the EXE out of the web accessible directory the web app resides in.

我应该关注任何安全方面吗?只有我能想到的是加密我的MSSQL连接字符串;当然,将EXE保持在Web应用程序所在的Web可访问目录之外。

Thank you in advance for the help/confirmation as I know some will think this is a silly question.

提前感谢您的帮助/确认,因为我知道有些人会认为这是一个愚蠢的问题。

3 个解决方案

#1


The only thing I can see you haven't considered is the identity under which Task Scheduler will run your EXE, but that shouldn't be too much of a concern seeing how you seem to control everything in the environment.

我唯一能看到你没有考虑的是Task Scheduler将运行你的EXE的身份,但是看起来你似乎控制环境中的一切并不是太过关注。

#2


As Esteban notes: will this just be running under your account and will you just stay signed on to the machine? If so, that is your biggest security risk. You can use a screen saver to force a login prompt and it is largely true that this is no less secure than logging out. However, I doubt that this would be considered "best practices."

正如Esteban所说:这只是在您的帐户下运行,您是否会继续登录该计算机?如果是这样,那是您最大的安全风险。您可以使用屏幕保护程序来强制登录提示,这一点确实不会比注销更安全。但是,我怀疑这会被视为“最佳做法”。

I would consider building a Windows Service that simply wakes up once a day. They are truly not very difficult to build although you'll want to be sure to have a good testing mechanism (an independent class that you compile into a Unit-testable dll is a good idea) since testing them once they are running is very difficult. But don't let the "mystery" of service apps or the idea of resource usage put you off. You'll be surprised how easy they are. As far as resources go...you are spending more resources keeping a full Winforms app running!

我会考虑构建一个每天只唤醒一次的Windows服务。它们真的不是很难构建,虽然你想要确保有一个好的测试机制(一个独立的类,你编译成一个单元可测试的dll是一个好主意),因为一旦它们运行就测试它们是非常困难的。但是,不要让服务应用程序的“神秘”或资源使用的想法让你失望。你会惊讶他们有多容易。就资源而言......你正在花费更多的资源来保持一个完整的Winforms应用程序运行!

#3


If you want to build a Windows Service, you can set it to start when the server (that the database resides on) starts. I'd have an external tracking system for sending the emails tho, possibly in a separate database. When the server starts, your service starts too. Service checks if an email has been sent today. If not, build email, send email, then mark it in the log (the db table I assume). Then have the service sleep for 24 hours, then have it check again (using an event). Repeat.

如果要构建Windows服务,可以将其设置为在服务器(数据库所在的服务器)启动时启动。我有一个外部跟踪系统,可以在一个单独的数据库中发送电子邮件。服务器启动时,您的服务也会启动。服务检查今天是否发送了电子邮件。如果没有,建立电子邮件,发送电子邮件,然后在日志中标记它(我假设的db表)。然后让服务休眠24小时,然后再次检查(使用事件)。重复。

#1


The only thing I can see you haven't considered is the identity under which Task Scheduler will run your EXE, but that shouldn't be too much of a concern seeing how you seem to control everything in the environment.

我唯一能看到你没有考虑的是Task Scheduler将运行你的EXE的身份,但是看起来你似乎控制环境中的一切并不是太过关注。

#2


As Esteban notes: will this just be running under your account and will you just stay signed on to the machine? If so, that is your biggest security risk. You can use a screen saver to force a login prompt and it is largely true that this is no less secure than logging out. However, I doubt that this would be considered "best practices."

正如Esteban所说:这只是在您的帐户下运行,您是否会继续登录该计算机?如果是这样,那是您最大的安全风险。您可以使用屏幕保护程序来强制登录提示,这一点确实不会比注销更安全。但是,我怀疑这会被视为“最佳做法”。

I would consider building a Windows Service that simply wakes up once a day. They are truly not very difficult to build although you'll want to be sure to have a good testing mechanism (an independent class that you compile into a Unit-testable dll is a good idea) since testing them once they are running is very difficult. But don't let the "mystery" of service apps or the idea of resource usage put you off. You'll be surprised how easy they are. As far as resources go...you are spending more resources keeping a full Winforms app running!

我会考虑构建一个每天只唤醒一次的Windows服务。它们真的不是很难构建,虽然你想要确保有一个好的测试机制(一个独立的类,你编译成一个单元可测试的dll是一个好主意),因为一旦它们运行就测试它们是非常困难的。但是,不要让服务应用程序的“神秘”或资源使用的想法让你失望。你会惊讶他们有多容易。就资源而言......你正在花费更多的资源来保持一个完整的Winforms应用程序运行!

#3


If you want to build a Windows Service, you can set it to start when the server (that the database resides on) starts. I'd have an external tracking system for sending the emails tho, possibly in a separate database. When the server starts, your service starts too. Service checks if an email has been sent today. If not, build email, send email, then mark it in the log (the db table I assume). Then have the service sleep for 24 hours, then have it check again (using an event). Repeat.

如果要构建Windows服务,可以将其设置为在服务器(数据库所在的服务器)启动时启动。我有一个外部跟踪系统,可以在一个单独的数据库中发送电子邮件。服务器启动时,您的服务也会启动。服务检查今天是否发送了电子邮件。如果没有,建立电子邮件,发送电子邮件,然后在日志中标记它(我假设的db表)。然后让服务休眠24小时,然后再次检查(使用事件)。重复。