创建C#服务以监视无限循环中的更改

创建C#服务以监视无限循环中的更改,c#,windows-services,C#,Windows Services,我的目标是看看Time1和Time2之间是否有任何变化。如果有更改,我需要发送一封电子邮件,并在另一个时间间隔内发生另一个更改之前跳过其余的检查 这是我第一次创建服务,我就是这么做的: protected override void OnStart(string[] args) { DateTime lastUpdate = new DateTime(); lastUpdate = new DateTime(2014, 01, 01, 00, 00

我的目标是看看Time1和Time2之间是否有任何变化。如果有更改,我需要发送一封电子邮件,并在另一个时间间隔内发生另一个更改之前跳过其余的检查

这是我第一次创建服务,我就是这么做的:

    protected override void OnStart(string[] args)
    {
        DateTime lastUpdate = new DateTime();
        lastUpdate = new DateTime(2014, 01, 01, 00, 00, 00);
        DateTime curTime = new DateTime();


        bool infLoop = true;
        while (infLoop)
        {                
            curTime = DateTime.Now;

            //define time range1
            DateTime startDateMorning = new DateTime(curTime.Year, curTime.Month, curTime.Day, 17, 40, 00);
            DateTime endDateMorning = new DateTime(curTime.Year, curTime.Month, curTime.Day, 17, 50, 00);

            //define time range2
            DateTime startDateEvening = new DateTime(curTime.Year, curTime.Month, curTime.Day, 10, 30, 00);
            DateTime endDateEvening = new DateTime(curTime.Year, curTime.Month, curTime.Day, 10, 40, 00);

            //time span between last update and current time in iteration
            TimeSpan span = curTime - lastUpdate;

            //check that we only monitor within interwals, and there was at least 20 minutes delay between last and current check
            if (((curTime >= startDateMorning && curTime < endDateMorning) 
                || 
                (curTime >= startDateEvening && curTime < endDateEvening)) 
                && span.Minutes > 20)
            {
                //connect to DataBase
                //get the value
                //email warning
                //other logic

                //set last uopdate as current timestamp
                lastUpdate = DateTime.Now;
            }
        }
    }
protected override void OnStart(字符串[]args)
{
DateTime lastUpdate=新的日期时间();
lastUpdate=新日期时间(2014,01,01,00,00,00);
DateTime curTime=新的DateTime();
bool infLoop=true;
while(infLoop)
{                
curTime=DateTime.Now;
//定义时间范围1
DateTime startDateMorning=新的日期时间(curTime.Year,curTime.Month,curTime.Day,17,40,00);
DateTime endDateMorning=新的日期时间(curTime.Year,curTime.Month,curTime.Day,17,50,00);
//定义时间范围2
DateTime StartDate黄昏=新的日期时间(curTime.Year,curTime.Month,curTime.Day,10,30,00);
DateTime enddatenight=新的日期时间(curTime.Year,curTime.Month,curTime.Day,10,40,00);
//迭代中上次更新和当前时间之间的时间跨度
TimeSpan=curTime-lastUpdate;
//检查我们是否只在间隔内进行监控,上次检查和当前检查之间至少有20分钟的延迟
如果(((curTime>=startDateMorning&&curTime=startdatenight和&curTime20分钟)
{
//连接到数据库
//获取值
//电子邮件警告
//其他逻辑
//将上次uUpdate设置为当前时间戳
lastUpdate=DateTime.Now;
}
}
}

当我使用sc create Service1 start=auto binPath=“c:\Users\....\program.exe”并试图运行它时,我的服务被卡住了。所以我必须寻找它并手动终止它。我认为我做得不对。

你需要使用
线程.Sleep()
来避免繁忙的等待,或者,更好的是,只要使用
系统.Timers.Timer
(或者它的一个表亲)在你想要的时候醒来。您还需要在
OnStart()
中启动服务,而不是尝试在那里完成所有工作;这意味着计划一个事件处理程序(例如通过
计时器
)或启动一个新线程,以便
OnStart()
可以返回其原始线程。Windows服务系统仅提供有限的启动时间
OnStop()
也需要实现正常关机

public partial class ServiceClassName : ServiceBase {
    private readonly Timer Ticker = new Timer {
                                               Interval = 5.0*TimeSpan.TicksPerMinute
                                                        /TimeSpan.TicksPerMillisecond
                                              }; // 5 minutes

    public ServiceClassName() {
        InitializeComponent();
        Ticker.Elapsed += (sender, e) => Poll();
    }

    protected override void OnStart(string[] args) { Ticker.Start(); }

    protected override void OnStop() { Ticker.Stop(); }

    internal static void Poll() {
        // approximate contents of your while loop
    }
}

您需要使用
Thread.Sleep()
来避免繁忙的等待,或者,更好的是,只需使用
System.Timers.Timer
(或它的同类产品)在您需要的时候醒来。您还需要在
OnStart()
中启动服务,而不是尝试在那里完成所有工作;这意味着计划一个事件处理程序(例如通过
计时器
)或启动一个新线程,以便
OnStart()
可以返回其原始线程。Windows服务系统仅提供有限的启动时间
OnStop()
也需要实现正常关机

public partial class ServiceClassName : ServiceBase {
    private readonly Timer Ticker = new Timer {
                                               Interval = 5.0*TimeSpan.TicksPerMinute
                                                        /TimeSpan.TicksPerMillisecond
                                              }; // 5 minutes

    public ServiceClassName() {
        InitializeComponent();
        Ticker.Elapsed += (sender, e) => Poll();
    }

    protected override void OnStart(string[] args) { Ticker.Start(); }

    protected override void OnStop() { Ticker.Stop(); }

    internal static void Poll() {
        // approximate contents of your while loop
    }
}
“我的服务卡住了”这句话不太能说明问题,但您需要尽快从OnStart返回。您还需要响应诸如OnStop之类的其他服务命令,这段代码无法响应,因为它被卡在了循环中

在单独的线程中运行代码,例如使用
任务

“我的服务卡住了”不太具有描述性,但您需要尽快从OnStart返回。您还需要响应诸如OnStop之类的其他服务命令,这段代码无法响应,因为它被卡在了循环中

在单独的线程中运行代码,例如使用
任务