简体   繁体   English

在特定时间执行特殊功能的Windows服务

[英]Windows Service that executes special function at specific time

I have a Windows Service that Monitors a Network folder for a file and on Creation moves that File and does some transfiguration. 我有一个Windows服务,它监视文件的Network文件夹,然后在Creation上移动该文件并进行一些变形。 This works great. 这非常有效。

My question is, if a file has not been saved to the Monitored Location by 9:30am I need to have my service send out a reminder email. 我的问题是,如果文件尚未在上午9:30之前保存到受监控位置,我需要让我的服务发送一封提醒邮件。 I was hoping not to have to write a 2nd program for this which is what seems to be a common suggestion(2nd program with Scheduled Task). 我希望不必为此编写第二个程序,这似乎是一个常见的建议(第二个程序与计划任务)。

So, what are my options? 那么,我的选择是什么? I assume that I can make this work using a Timer in some fashion but I understand that is not the best idea. 我认为我可以用某种方式使用Timer来完成这项工作,但我知道这不是最好的主意。 Is there a way to have a Scheduled Task make my Service execute a specific function? 有没有办法让计划任务使我的服务执行特定的功能?

Thanks for any ideas... 谢谢你的任何想法......

EDIT: for reference I am currently researching Quartz.NET , just seemed like overkill at first... 编辑:作为参考我目前正在研究Quartz.NET起初看起来有点矫枉过正......

我没有看到使用计时器有什么不对,使用System.Timers.Timer并设置间隔为例如5分钟,然后在Elapsed事件检查当前时间,如果9:30传递发送邮件。

Personnaly, I'd suggest System.Threading.Timer in the same service. Personnaly,我建议在同一服务中使用System.Threading.Timer

Something like this: 像这样的东西:

using System;
using System.Threading;

public class Foo
{
    private Timer _timer;
    private readonly static TimeSpan TIME_OF_DAY = new TimeSpan(9, 30, 0);

    public void Main()
    {
        var now = DateTime.Now;
        var nextDt = now.Date.AddDays(1).Add(TIME_OF_DAY); 
        _timer = new Timer(TimerCallback, null, (int)nextDt.Subtract(now).TotalMilliseconds, Timeout.Infinite);
    }

    public void TimerCallback(object state)
    {
        try
        {
            // Reminder
        }
        finally
        {
            try
            {
                var now = DateTime.Now;
                var nextDt = now.Date.AddDays(1).Add(TIME_OF_DAY);
                _timer.Change((int)nextDt.Subtract(now).TotalMilliseconds, Timeout.Infinite);
            }
            catch (ObjectDisposedException) { }
        }
    }
}

I would also recommend a second program to do the checking and to send the email since it's not easy to communicate with a service. 我还建议第二个程序来检查和发送电子邮件,因为它不容易与服务通信。 It may seem like duplication but it will be easier to develop and maintain in the long run. 这似乎是重复,但从长远来看,它将更容易开发和维护。

Rather than having the service send the email, have the second program send the email. 第二个程序发送电子邮件,而不是让服务发送电子邮件。 If you want to reuse code from the service, add the service project as a reference to the second program. 如果要重用服务中的代码,请添加服务项目作为对第二个程序的引用。

That may be easier said than done but it should be easier than trying to have the second program communicate with the service in some way since you'd have to wire the communication mechanism yourself. 这说起来容易做起来难,但它应该比尝试让第二个程序以某种方式与服务进行通信更容易,因为你必须自己连接通信机制。

You have two questions in your post. 你的帖子中有两个问题。 1. You need a scheduler to check at 9.30 AM everyday 2. If it is not saved you need to notify via email. 1.您需要一个调度程序在每天上午9:30检查2.如果没有保存,您需要通过电子邮件通知。

Actually Quartz.NET is very simple to use. 实际上Quartz.NET使用起来非常简单。 I am currently using Quartz.NET for one of my schedule tasks to run. 我目前正在使用Quartz.NET来运行我的一个计划任务。 You just need to write 4-5 lines of code and have the job scheduling part done. 您只需编写4-5行代码并完成作业调度部分。

Coming to your email you can use log4net. 来到您的电子邮件,您可以使用log4net。 Log4net has something called SMTPAppender. Log4net有一个名为SMTPAppender的东西。 All your configurations related to log4net you can specify in your web or app.config itself. 您可以在web或app.config中指定与log4net相关的所有配置。 Its also very simple and pretty straight forward. 它也很简单,非常直接。 A sample log4net configuration for SMTPAppender will look like below: SMTPAppender的示例log4net配置如下所示:

<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
  </configSections>
  <log4net>
    <appender name="SMTPAppender" type="log4net.Appender.SMTPAppender">
      <authentication value="Basic" />
      <to value="xxx@xxx" />
      <from value="yyy@yyy" />
      <username value="user" />
      <password value="pass" />
      <subject value="ERROR" />
      <smtpHost value="host" />
      <port value="25" />
      <lossy value="true" />
      <evaluator type="log4net.Core.LevelEvaluator">
        <threshold value="WARN" />
      </evaluator>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%newline%date [%thread] %-5level %logger %newline %message%newline%newline%newline" />
      </layout>
    </appender>
    <root>
      <level value="INFO"></level>
    </root>
    <logger name="SMTPAppender">
      <level value="INFO"></level>
      <appender-ref ref="SMTPAppender"></appender-ref>
    </logger>
  </log4net>
</configuration>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM